package com.jwsoft.manager.core.integration.individuation.helper;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bifang.assistant.common.vo.SessionInfo;
import com.bifang.engine.assistant.core.enums.AuthStatusEnum;
import com.bifang.engine.assistant.core.enums.BoolEnum;
import com.bifang.engine.assistant.core.exception.AppException;
import com.bifang.engine.assistant.core.utils.SessionUtil;
import com.bifang.engine.cache.redis.RedisHelper;
import com.bifang.module.base.common.vo.common.CommonVO;
import com.bifang.module.base.core.integration.CommonIntegration;
import com.github.pagehelper.PageInfo;
import com.jwsoft.manager.common.constant.EduCache;
import com.jwsoft.manager.common.enums.CardTypeEnum;
import com.jwsoft.manager.common.enums.EnrollPreTypeEnum;
import com.jwsoft.manager.common.enums.EnrollStatusFormalEnum;
import com.jwsoft.manager.common.enums.SchoolNatureEnum;
import com.jwsoft.manager.common.vo.cache.EduOperationCacheDetailVO;
import com.jwsoft.manager.common.vo.cache.EduOperationCacheQueryVO;
import com.jwsoft.manager.common.vo.cache.EduOperationCacheVO;
import com.jwsoft.manager.common.vo.eduAppointStudent.EduAppointStudentVO;
import com.jwsoft.manager.common.vo.eduCompany.EduCompanyVO;
import com.jwsoft.manager.common.vo.eduFormEnroll.EduFormEnrollVO;
import com.jwsoft.manager.common.vo.eduFormEnrollParams.EduFormEnrollParamsVO;
import com.jwsoft.manager.common.vo.eduFormSpecial.EduFormSpecialQueryVO;
import com.jwsoft.manager.common.vo.eduFormSpecial.EduFormSpecialVO;
import com.jwsoft.manager.common.vo.eduHouse.EduHouseVO;
import com.jwsoft.manager.common.vo.eduHouseDetail.EduHouseDetailVO;
import com.jwsoft.manager.common.vo.eduJhjyQuerySync.EduJhjyQuerySyncVO;
import com.jwsoft.manager.common.vo.eduLivePlace.EduLivePlaceVO;
import com.jwsoft.manager.common.vo.eduNetsign.EduNetsignVO;
import com.jwsoft.manager.common.vo.eduPrivateAppointStudent.EduPrivateAppointStudentVO;
import com.jwsoft.manager.common.vo.eduSchool.EduSchoolVO;
import com.jwsoft.manager.common.vo.eduSchoolConfig.EduSchoolConfigVO;
import com.jwsoft.manager.common.vo.eduSocialSecurity.EduSocialSecurityVO;
import com.jwsoft.manager.common.vo.eduStudentEnroll.ApplySchoolVO;
import com.jwsoft.manager.common.vo.eduStudentEnroll.SaveStudentEnrollVO;
import com.jwsoft.manager.common.vo.eduStudentEnrollBefore.EduStudentEnrollBeforeVO;
import com.jwsoft.manager.common.vo.eduStudentEnrollPre.EduStudentEnrollPreKeyVO;
import com.jwsoft.manager.common.vo.eduStudentEnrollPre.EduStudentEnrollPreQueryVO;
import com.jwsoft.manager.common.vo.eduStudentEnrollPre.EduStudentEnrollPreVO;
import com.jwsoft.manager.core.dao.model.*;
import com.jwsoft.manager.core.dao.service.*;
import com.jwsoft.manager.core.integration.EduJhjyQuerySyncIntegration;
import com.jwsoft.manager.core.integration.EduStudentEnrollIntegration;
import com.jwsoft.manager.core.integration.EduSystemConfigIntegration;
import com.jwsoft.manager.core.integration.impl.EduStudentEnrollPreIntegrationImpl;
import com.jwsoft.manager.core.util.EduHelper;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 东阳生成报名工具类
 * **/
@Component
public class DyEnrollHelper {
    @Autowired
    private EduStudentEnrollIntegration eduStudentEnrollIntegration;
    @Autowired
    private EduFormSpecialService eduFormSpecialService;
    @Autowired
    private EduZsbAbandonService eduZsbAbandonService;

    @Autowired
    private EduSchoolBbService eduSchoolBbService;
    @Autowired
    private EduHouseholdService eduHouseholdService;
    @Autowired
    public EduJhjyQuerySyncIntegration eduJhjyQuerySyncIntegration;
    @Autowired
    private EduSchoolService eduSchoolService;
    @Autowired
    private EduBreakThroughService eduBreakThroughService;

    @Autowired
    private CommonIntegration commonIntegration;
    @Autowired
    public EduStudentEnrollFamilyService eduStudentEnrollFamilyService;
    @Autowired
    private EduSystemConfigIntegration eduSystemConfigIntegration;
    @Autowired
    private EduStudentEnrollService eduStudentEnrollService;
    @Autowired
    private EduFormEnrollService eduFormEnrollService;
    @Autowired
    private EduSchoolVacancyService eduSchoolVacancyService;
    @Autowired
    private EduStudentEnrollPreIntegrationImpl eduStudentEnrollPreIntegration;
    @Autowired
    private RedisHelper redisHelper;
    @Autowired
    RedissonClient redissonClient;
    @Autowired
    EduHelper eduHelper;
    @Autowired
    private EduStudentEnrollBlacklistService eduStudentEnrollBlacklistService;
    @Autowired
    private EduSchoolLrService eduSchoolLrService;
    @Autowired
    private  EduEnrollmenBrochureService eduEnrollmenBrochureService;
    @Autowired
    private EduSchoolConfigService eduSchoolConfigService;
    @Autowired
    private EduStudentEnrollWhiteService eduStudentEnrollWhiteService;

    public String save(SaveStudentEnrollVO vo) {
        String bmKey = EduCache.EDU_ENROLL_ZS + vo.getIdcard();
        if (redisHelper.hasKey(bmKey)) {
            throw new AppException("报名保存处理中，如遇到问题，请等待10分钟后再试");
        }
        redisHelper.set(bmKey, true, 600);
        try{
            checkSave(vo);
        }catch (AppException e){
            redisHelper.del(bmKey);
            throw e;
        }catch (Exception e){
            redisHelper.del(bmKey);
            throw new AppException("系统异常",e);
        }
        EduStudentEnroll eduStudentEnroll=Convert.convert(EduStudentEnroll.class,vo);
        //判断是否随迁
        if (StringUtils.isBlank(eduStudentEnroll.getFollowingFlag())) {
            QueryWrapper<EduHousehold> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(EduHousehold::getYear, eduStudentEnroll.getYear())
                    .eq(EduHousehold::getIdcard, eduStudentEnroll.getIdcard())
                    .eq(EduHousehold::getAuthStatus, AuthStatusEnum.YES.getType())
                    .eq(EduHousehold::getAreaCode, eduStudentEnroll.getAreaCode());
            if (eduHouseholdService.count(queryWrapper) > 0) {
                //有户籍认为非随迁
                eduStudentEnroll.setFollowingFlag(BoolEnum.TRUE.getType());
            } else {
                if (BoolEnum.TRUE.getType().equalsIgnoreCase(eduStudentEnroll.getSpecialFlag())) {
                    //特殊通道认为非随迁
                    eduStudentEnroll.setFollowingFlag(BoolEnum.TRUE.getType());
                } else {
                    if ("A".equalsIgnoreCase(eduStudentEnroll.getConfigName()) || "B".equalsIgnoreCase(eduStudentEnroll.getConfigName())) {
                        //A类B类认为是非随迁
                        eduStudentEnroll.setFollowingFlag(BoolEnum.TRUE.getType());
                    } else {
                        eduStudentEnroll.setFollowingFlag(BoolEnum.FALSE.getType());
                    }
                }
            }
        }
        //设置为待审核状态
        if(vo.getLrFlag()){

            //设置数据
            eduStudentEnroll.setConfigName(vo.getConfigName());
            eduStudentEnroll.setConfigTypeName(vo.getConfigTypeName());
            eduStudentEnroll.setFormId(vo.getFormId());
            eduStudentEnroll.setRemarks(vo.getRemarks());
            eduStudentEnroll.setSource("6");//学校录入
            eduStudentEnroll.setParam20(vo.getParam20());
            eduStudentEnroll.setDeployFlag(BoolEnum.FALSE.getType());
            eduStudentEnroll.setAdmitFlag(BoolEnum.TRUE.getType());
            eduStudentEnroll.setAuthStatus(AuthStatusEnum.YES.getType());
            eduStudentEnroll.setAuthUserId(SessionUtil.getUserId());
            eduStudentEnroll.setAuthTime(new Date());
            eduStudentEnroll.setEnrollStatus(EnrollStatusFormalEnum.lq.getType());
            eduStudentEnroll.setAdmitTime(new Date());
            eduStudentEnroll.setAspirationType("1");
            eduStudentEnroll.setAdmitRemarks("学校直接录入录取");
            //修改其他学校为无效
            List<String> enrollStatus1List = new ArrayList<>();
            enrollStatus1List.add(EnrollStatusFormalEnum.dsh.getType());
            enrollStatus1List.add(EnrollStatusFormalEnum.dlq.getType());
            QueryWrapper<EduStudentEnroll> otherQuery = new QueryWrapper<>();
            otherQuery.lambda().ne(EduStudentEnroll::getSchoolId, eduStudentEnroll.getSchoolId())
                    .eq(EduStudentEnroll::getIdcard, eduStudentEnroll.getIdcard())
                    .eq(EduStudentEnroll::getYear, eduStudentEnroll.getYear())
                    .in(EduStudentEnroll::getEnrollStatus, enrollStatus1List);
            List<EduStudentEnroll> otherList = eduStudentEnrollService.list(otherQuery);
            if (!CollectionUtils.isEmpty(otherList)) {
                for (EduStudentEnroll otherEnroll : otherList) {
                    otherEnroll.setEnrollStatus(EnrollStatusFormalEnum.ywblq.getType());
                    otherEnroll.setAdmitFlag(BoolEnum.FALSE.getType());
                    otherEnroll.setAdmitRemarks("因其他优先级高的学校录取，系统自动设置为不录取");
                    if (otherEnroll.getAuthStatus().equals(AuthStatusEnum.NODO.getType())) {
                        otherEnroll.setAuthStatus(AuthStatusEnum.NO.getType());
                        otherEnroll.setAuthRemarks("因其他优先级高的学校录取，系统自动设置为审核不通过");
                        otherEnroll.setAuthUserId(SessionUtil.getSessionInfo().getUserId());
                    }
                    otherEnroll.setAdmitUserId(SessionUtil.getSessionInfo().getUserId());
                    eduStudentEnrollService.updateById(otherEnroll);
                }
            }

            //查询所属学校配置表
            QueryWrapper<EduSchoolConfig> configQueryWrapper = new QueryWrapper<>();
            configQueryWrapper.lambda().select(EduSchoolConfig::getEnrolment, EduSchoolConfig::getSerialNumEnd)
                    .eq(EduSchoolConfig::getSchoolId, vo.getSchoolId())
                    .eq(EduSchoolConfig::getAuthStatus, AuthStatusEnum.YES.getType())
                    .eq(EduSchoolConfig::getStatus, BoolEnum.TRUE.getType())
                    .eq(EduSchoolConfig::getYear, vo.getYear());
            if ("1".equals(vo.getSchoolNature()) || "2".equals(vo.getSchoolNature())) {
                configQueryWrapper.lambda().eq(EduSchoolConfig::getConfigTypeName, vo.getConfigTypeName());
            }
            EduSchoolConfig eduSchoolConfig = eduSchoolConfigService.getOne(configQueryWrapper, false);
            if (StringUtils.isBlank(eduStudentEnroll.getApplyId())) {
                //设置applYId
                Integer orderNo = 0;
                if ("0".equals(eduStudentEnroll.getSchoolNature())) {
                    if (StringUtils.isBlank(eduStudentEnroll.getFormId())) {
                        throw new AppException("报名表单id不能为空");
                    }
                    EduFormEnroll eduFormEnroll = eduFormEnrollService.getById(eduStudentEnroll.getFormId());
                    eduStudentEnroll.setSpecialFlag(BoolEnum.FALSE.getType());
                    if (eduFormEnroll == null) {
                        EduFormSpecial eduFormSpecial = eduFormSpecialService.getById(eduStudentEnroll.getFormId());
                        if (eduFormSpecial == null) {
                            throw new AppException("未匹配到学校配置的报名表单，如果疑问，请拨打学校电话了解详细情况");
                        }
                        orderNo = 99;
                        eduStudentEnroll.setConfigTypeName(eduFormSpecial.getBigClassValue());
                        eduStudentEnroll.setSpecialFlag(BoolEnum.TRUE.getType());

                    }else{
                        if (!eduFormEnroll.getValidStatus().equals(BoolEnum.TRUE.getType())) {
                            throw new AppException("学校配置报名表单已无效，如果疑问，请拨打学校电话了解详细情况");
                        }
                        orderNo = eduFormEnroll.getOrderNo();
                        //判断年龄是否符合，不是特殊通道类别，需要判断年龄
                        if (StringUtils.isBlank(vo.getBirthYmd())) {
                            throw new AppException("出生日期不能为空");
                        }
                        String birthday = "";
                        if (vo.getBirthYmd().length() >= 8) {
                            birthday = vo.getBirthYmd().replaceAll("-", "").replaceAll("/", "");
                        } else if (vo.getBirthYmd().length() == 6) {
                            birthday = vo.getBirthYmd() + "01";
                        } else {
                            throw new AppException("出生日期格式不正确");
                        }
                        Integer birthYmd = Integer.parseInt(birthday);
                        if (eduSchoolConfig.getStartAge() != null) {
                            Integer startBirthday =
                                    Integer.parseInt(
                                            DatePattern.PURE_DATE_FORMAT.format(eduSchoolConfig.getStartAge()));
                            if (birthYmd < startBirthday) {
                                throw new AppException("年龄不符，如有疑问，请拨打学校电话了解详细情况");
                            }
                        }
                        if (eduSchoolConfig.getEndAge() != null) {
                            Integer endBirthday =
                                    Integer.parseInt(
                                            DatePattern.PURE_DATE_FORMAT.format(eduSchoolConfig.getEndAge()));
                            if (birthYmd > endBirthday) {
                                throw new AppException("年龄不符，如有疑问，请拨打学校电话了解详细情况");
                            }
                        }
                    }
                } else {
                    if (StringUtils.isBlank(eduStudentEnroll.getConfigTypeName())) {
                        throw new AppException(eduStudentEnroll.getSchoolName() + "的报名类别不能为空");
                    }
                    QueryWrapper<EduSchoolConfig> schoolConfigMbQueryWrapper = new QueryWrapper<>();
                    schoolConfigMbQueryWrapper
                            .lambda()
                            .eq(EduSchoolConfig::getSchoolId, eduStudentEnroll.getSchoolId())
                            .eq(EduSchoolConfig::getAuthStatus, AuthStatusEnum.YES.getType())
                            .eq(EduSchoolConfig::getStatus, BoolEnum.TRUE.getType())
                            .eq(EduSchoolConfig::getConfigTypeName, eduStudentEnroll.getConfigTypeName())
                            .eq(EduSchoolConfig::getYear, eduStudentEnroll.getYear());
                    EduSchoolConfig schoolConfigMb = eduSchoolConfigService.getOne(schoolConfigMbQueryWrapper, false);
                    if (schoolConfigMb == null) {
                        throw new AppException(eduStudentEnroll.getYear() + "年度报名学校报名类别不匹配，如有疑问，请拨打学校电话了解详细情况");
                    }
                    if (schoolConfigMb.getOrderNo() == null) {
                        throw new AppException(eduStudentEnroll.getYear() + "年度报名学校招生计划设置有误，如有疑问，请拨打学校电话了解详细情况");
                    }
                    orderNo = schoolConfigMb.getOrderNo();
                }
                vo.setFormOrderNo(orderNo);
                //生成applyId
                eduStudentEnroll.setApplyId(this.getApplyId(vo.getYear(), vo.getSchoolNature(), vo.getSchoolNum(), vo.getFormOrderNo(), vo.getSchoolId(), 1));
            }

            //判断是否有与采集数据，没有则新增,有则判断是否修改
            EduStudentEnrollPreQueryVO queryVO = new EduStudentEnrollPreQueryVO();
            queryVO.setYear(vo.getYear());
            queryVO.setIdcard(vo.getIdcard());
            queryVO.setAuthStatus(AuthStatusEnum.YES.getType());
            PageInfo<EduStudentEnrollPreVO> list = eduStudentEnrollPreIntegration.getList(queryVO);
            List<EduStudentEnrollPreVO> preList = list.getList();

            if (ObjectUtils.isEmpty(preList)) {
                // 需要新增预采集信息
                EduStudentEnrollPreVO eduStudentEnrollPreVO = createNewEnrollPreVO(vo);
                try {
                    eduStudentEnrollPreIntegration.add(eduStudentEnrollPreVO);
                } catch (Exception e) {
                    redisHelper.del(bmKey);
                    throw new RuntimeException(e);
                }
            } else {
                // 修改与采集信息
                EduStudentEnrollPreVO studentEnrollPre = preList.get(0);
                boolean isUpdate = false;
                try {
                    isUpdate = updateEnrollPreVO(studentEnrollPre, vo);
                } catch (Exception e) {
                    redisHelper.del(bmKey);
                    throw new RuntimeException(e);
                }
                if (isUpdate) {
                    try {
                        studentEnrollPre.setEnrollPreType(EnrollPreTypeEnum.xxlr.getType());
                        eduStudentEnrollPreIntegration.edit(studentEnrollPre);
                    } catch (Exception e) {
                        redisHelper.del(bmKey);
                        throw new RuntimeException(e);
                    }
                }
            }
            //判断是否随迁
            if (StringUtils.isBlank(eduStudentEnroll.getFollowingFlag())) {
                QueryWrapper<EduHousehold> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(EduHousehold::getYear, eduStudentEnroll.getYear())
                        .eq(EduHousehold::getIdcard, eduStudentEnroll.getIdcard())
                        .eq(EduHousehold::getAuthStatus, AuthStatusEnum.YES.getType())
                        .eq(EduHousehold::getAreaCode, eduStudentEnroll.getAreaCode());
                if (eduHouseholdService.count(queryWrapper) > 0) {
                    //有户籍认为非随迁
                    eduStudentEnroll.setFollowingFlag(BoolEnum.TRUE.getType());
                } else {
                    if (BoolEnum.TRUE.getType().equalsIgnoreCase(eduStudentEnroll.getSpecialFlag())) {
                        //特殊通道认为非随迁
                        eduStudentEnroll.setFollowingFlag(BoolEnum.TRUE.getType());
                    } else {
                        if ("A".equalsIgnoreCase(eduStudentEnroll.getConfigName()) || "B".equalsIgnoreCase(eduStudentEnroll.getConfigName())) {
                            //A类B类认为是非随迁
                            eduStudentEnroll.setFollowingFlag(BoolEnum.TRUE.getType());
                        } else {
                            eduStudentEnroll.setFollowingFlag(BoolEnum.FALSE.getType());
                        }
                    }
                }
            }
            //录入
            try {
                eduStudentEnrollService.saveOrUpdate(eduStudentEnroll);
            } catch (Exception e) {
                throw new AppException("报名编号重复，请重新点击保存");
            }
            return eduStudentEnroll.getApplyId();
        }else{
            eduStudentEnroll.setEnrollStatus(EnrollStatusFormalEnum.dsh.getType());
            eduStudentEnroll.setAuthStatus(AuthStatusEnum.NODO.getType());
            eduStudentEnroll.setStudentEnrollId(IdUtil.simpleUUID());
            bindIdcard(eduStudentEnroll,vo.getBindIdcardList());
            if (!CollectionUtils.isEmpty(vo.getOtherSchoolList()) && !vo.getZsbEnrollFlag()) {
                for(ApplySchoolVO applySchoolVO:vo.getOtherSchoolList()){

                    String applyId="";
                    EduStudentEnroll studentEnroll=new EduStudentEnroll();
                    BeanUtil.copyProperties(eduStudentEnroll,studentEnroll);
                    studentEnroll.setStudentEnrollId(IdUtil.simpleUUID());
                    studentEnroll.setParentId(eduStudentEnroll.getStudentEnrollId());
                    studentEnroll.setConfigTypeName(applySchoolVO.getConfigTypeName());
                    studentEnroll.setConfigName(applySchoolVO.getConfigName());
                    studentEnroll.setSchoolId(applySchoolVO.getSchoolId());
                    studentEnroll.setSchoolCategory(applySchoolVO.getSchoolCategory());
                    studentEnroll.setSchoolNature(applySchoolVO.getSchoolNature());
                    studentEnroll.setSchoolName(applySchoolVO.getSchoolName());
                    studentEnroll.setStreet(applySchoolVO.getStreet());
                    studentEnroll.setBindIdcard(applySchoolVO.getBindIdcard());
                    studentEnroll.setBindFlag(applySchoolVO.getBindFlag());
                    studentEnroll.setBindMianFlag(applySchoolVO.getBindMianFlag());
                    //民办城区E设置为空
                    studentEnroll.setAppointFlag(null);
                    studentEnroll.setAppointStudentId(null);
                    studentEnroll.setAspirationType(applySchoolVO.getAspirationType());
                    studentEnroll.setAreaCode(applySchoolVO.getAreaCode());
                    //摇号绑定处理
                    bindIdcard(studentEnroll,applySchoolVO.getBindIdcardList());
                    //获取编号
                    applyId=this.getApplyId(vo.getYear(),applySchoolVO.getSchoolNature(),applySchoolVO.getSchoolNum(),applySchoolVO.getOrderNo(),applySchoolVO.getSchoolId(),1);
                    studentEnroll.setApplyId(applyId);
                    try {
                        eduStudentEnrollService.save(studentEnroll);
                    }catch (Exception e){
                        redisHelper.del(bmKey);
                        throw new AppException("系统繁忙，请多点几次提交");
                    }
                }

            }

            //公办直升班信息设置为空
            if("0".equals(vo.getSchoolNature())){
                eduStudentEnroll.setPrivateAppointFlag(null);
                eduStudentEnroll.setPrivateAppointStudentId(null);
            }
            //获取报名编号
            RLock lock = redissonClient.getLock(EduCache.EDU_YW_ENROLL + vo.getSchoolId());
            boolean res;
            try {
                //等待锁10s,锁租约时间20s
                res = lock.tryLock(3000, 20000, TimeUnit.MILLISECONDS);
            } catch (InterruptedException e) {
                redisHelper.del(bmKey);
                throw new AppException("系统繁忙，请稍后再试", e);
            }
            String applyId =null;
            if (res) {
                try {
                    applyId = this.getApplyId(vo.getYear(), vo.getSchoolNature(), vo.getSchoolNum(), vo.getFormOrderNo(), vo.getSchoolId(), 1);
                    eduStudentEnroll.setApplyId(applyId);
                    eduStudentEnroll.setAspirationType("1");
                    eduStudentEnrollService.save(eduStudentEnroll);
                    redisHelper.set(bmKey, true, 10);

                } catch (Exception e) {
                    if("0".equals(eduStudentEnroll.getSchoolNature())){
                        String redisKey="ywEnroll:gb:"+vo.getSchoolNature()+":"+vo.getFormOrderNo();
                        redisHelper.del(redisKey);
                    }
                    redisHelper.del(bmKey);
                    throw new AppException("系统繁忙，请多点几次提交");
                }finally {
                    lock.unlock();
                }
            }else{
                redisHelper.del(bmKey);
                throw new AppException("当前时间段该学校报名人数过多，系统繁忙，请稍后再试");
            }
            if(StringUtils.isNotBlank(vo.getAbandonZsbFlag())&&BoolEnum.TRUE.getType().equals(vo.getAbandonZsbFlag())){
                //放弃直升班保存
                EduZsbAbandon eduZsbAbandon=new EduZsbAbandon();
                eduZsbAbandon.setAbandonId(IdUtil.simpleUUID());
                eduZsbAbandon.setUserId(SessionUtil.getUserId());
                eduZsbAbandon.setFullName(eduStudentEnroll.getFullName());
                eduZsbAbandon.setIdcard(eduStudentEnroll.getIdcard());
                eduZsbAbandon.setAbandonFullName(SessionUtil.getFullName());
                eduZsbAbandon.setAbandonIdcard(SessionUtil.getSessionInfo()!=null?SessionUtil.getSessionInfo().getIdcard():"");
                eduZsbAbandonService.save(eduZsbAbandon);
            }
            return  applyId;
        }
    }

    private boolean updateEnrollPreVO(EduStudentEnrollPreVO studentEnrollPre, SaveStudentEnrollVO vo) {
        boolean isUpdate = false;
        //需要校验的属性
        String[] propertiesToCheck = {
                "idcard", "cardType", "fullName", "fatherName", "fatherCardType",
                "fatherIdcard", "motherName", "motherCardType", "motherIdcard",
                "fatherCellphone", "motherCellphone"
        };
        for (String property : propertiesToCheck) {
            try {
                //比对是否有修改
                if (areValuesDifferent(studentEnrollPre, vo, property)) {
                    isUpdate = true;
                    setPropertyValue(studentEnrollPre, vo, property);
                }
            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                throw new AppException("学校线下录入比对字段有误",e);
            }
        }
        return isUpdate;
    }
    private void setPropertyValue(EduStudentEnrollPreVO obj1, SaveStudentEnrollVO obj2, String property)
            throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        try {
            // 使用反射获取属性值
            Method getterVO = SaveStudentEnrollVO.class.getMethod("get" + property.substring(0, 1).toUpperCase() + property.substring(1));
            Object value2 = getterVO.invoke(obj2);
            // 使用反射设置属性值
            Method setter = EduStudentEnrollPreVO.class.getMethod("set" + property.substring(0, 1).toUpperCase() + property.substring(1), value2.getClass());
            setter.invoke(obj1, value2);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            throw new AppException(e); // 处理反射异常
        }
    }
    private boolean areValuesDifferent(EduStudentEnrollPreVO obj1, SaveStudentEnrollVO  obj2, String property)
            throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        if (obj1 == null && obj2 == null) {
            return false;
        }
        if (obj1 == null || obj2 == null) {
            //有一方为空，不去比对，表示需要修改
            return true;
        }
        try {
            //发射获取EduStudentEnrollPreVO的属性值
            Method getter = EduStudentEnrollPreVO.class.getMethod("get" + property.substring(0, 1).toUpperCase() + property.substring(1));
            Object value1 = getter.invoke(obj1);
            //发射获取SaveStudentEnrollVO的属性值
            Method getterVO = SaveStudentEnrollVO.class.getMethod("get" + property.substring(0, 1).toUpperCase() + property.substring(1));
            Object value2 = getterVO.invoke(obj2);

            return !Objects.equals(value1, value2);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
             return false;
        }
    }

    private EduStudentEnrollPreVO createNewEnrollPreVO(SaveStudentEnrollVO vo) {
        EduStudentEnrollPreVO enrollPreVO = new EduStudentEnrollPreVO();
        enrollPreVO.setManagerFlag(true);
        enrollPreVO.setIdcard(vo.getIdcard());
        enrollPreVO.setCardType(vo.getCardType());
        enrollPreVO.setFullName(vo.getFullName());
        enrollPreVO.setFatherName(vo.getFatherName());
        enrollPreVO.setFatherCardType(vo.getFatherCardType());
        enrollPreVO.setFatherIdcard(vo.getFatherIdcard());
        enrollPreVO.setMotherName(vo.getMotherName());
        enrollPreVO.setMotherCardType(vo.getMotherCardType());
        enrollPreVO.setMotherIdcard(vo.getMotherIdcard());
        enrollPreVO.setSchoolCategory(vo.getSchoolCategory());
        enrollPreVO.setMotherCellphone(vo.getMotherCellphone());
        enrollPreVO.setFatherCellphone(vo.getFatherCellphone());
        enrollPreVO.setYear(vo.getYear());

        enrollPreVO.setSex(vo.getSex());
        enrollPreVO.setBirthYmd(vo.getBirthYmd());

        //设置管理员录入
        enrollPreVO.setEnrollPreType(EnrollPreTypeEnum.gl.getType());
        return enrollPreVO;
    }

    public  String getApplyId(Integer year,String schoolNature,String schoolNum,Integer orderNo,String shcoolId,Integer count){
        if(count==null){
            count=count+1;
        }
        if(count>10){
            //超过10次未能比对到不重复的数据，返回报错
            throw new AppException("系统繁忙，请重新点提交");
        }
        String y=year % 100+"";
        String num = StringUtils.leftPad(schoolNum, 3, "0");
        String orderNoId = StringUtils.leftPad(orderNo + "", 2, "0");
        String applyId=y+num+orderNoId;
        if("0".equals(schoolNature)){
            String redisKey="ywEnroll:gb:"+shcoolId+":"+orderNo;
            if(redisHelper.hasKey(redisKey)) {
                String redisApplyId= (String) redisHelper.get(redisKey);
                applyId=String.valueOf(Long.parseLong(redisApplyId)+1);
                redisHelper.set(redisKey,applyId,259200);
            }else{
                //公办
                String max=eduStudentEnrollService.queryMaxApplyId(applyId,shcoolId,year);
                if(StringUtils.isBlank(max)){
                    applyId=applyId+StringUtils.leftPad("1", 3, "0");
                }else{
                    applyId=String.valueOf(Long.parseLong(max)+1);
                }
                redisHelper.set(redisKey,applyId,259200);
            }
            return applyId;
        }else{
            //民办或者公参民转公
            String redisKey="ywEnroll:mb:"+shcoolId+":"+orderNo;
            Integer randomId =null;
            if(redisHelper.hasKey(redisKey)) {

                List<Integer> randomList= (List<Integer>) redisHelper.get(redisKey);
                if(CollectionUtils.isEmpty(randomList)){
                    Integer[] randomNums= NumberUtil.generateBySet(1,9999,3000);
                    randomList=Arrays.stream(randomNums).distinct().collect(Collectors.toList());
                    randomId=randomList.get(0);
                    randomList.remove(randomId);
                    redisHelper.set(redisKey,randomList,259200);
                }else{
                    randomId=randomList.get(0);
                    randomList.remove(randomId);
                    redisHelper.set(redisKey,randomList,259200);
                }

            }else{
                Integer[] randomNums=NumberUtil.generateBySet(1,9999,3000);
                List<Integer> randomList=Arrays.stream(randomNums).distinct().collect(Collectors.toList());
                randomId=randomList.get(0);
                randomList.remove(randomId);
                redisHelper.set(redisKey,randomList,259200);
            }

            applyId=applyId+StringUtils.leftPad(randomId + "", 4, "0");

        }
        QueryWrapper<EduStudentEnroll> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().select(EduStudentEnroll::getStudentEnrollId).eq(EduStudentEnroll::getSchoolId,shcoolId)
                .eq(EduStudentEnroll::getYear,year)
                .eq(EduStudentEnroll::getApplyId,applyId);
        if(eduStudentEnrollService.count(queryWrapper)>0){
            applyId=this.getApplyId(year,schoolNature,schoolNum,orderNo,shcoolId,count);
        }
        return applyId;

    }
    //处理绑定摇号
    private void bindIdcard(EduStudentEnroll studentEnroll,List<String> bindIdcardList){
        if(!BoolEnum.TRUE.getType().equalsIgnoreCase(studentEnroll.getBindFlag())){
            return;
        }
        if(CollectionUtils.isEmpty(bindIdcardList)){
            throw new AppException("绑定的学生身份证不能为空");
        }
        QueryWrapper<EduStudentEnroll> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda()
                .in(EduStudentEnroll::getIdcard,bindIdcardList)
                .eq(EduStudentEnroll::getYear,studentEnroll.getYear())
                .eq(EduStudentEnroll::getSchoolId,studentEnroll.getSchoolId())
                .eq(EduStudentEnroll::getEnrollStatus,EnrollStatusFormalEnum.dsh.getType())
                .eq(SchoolNatureEnum.mb.getType().equalsIgnoreCase(studentEnroll.getSchoolNature())||SchoolNatureEnum.mzg.getType().equalsIgnoreCase(studentEnroll.getSchoolNature()),EduStudentEnroll::getConfigTypeName,studentEnroll.getConfigTypeName())
        .orderByAsc(EduStudentEnroll::getCreateTime,EduStudentEnroll::getApplyId);
        List<EduStudentEnroll> eduStudentEnrolls=eduStudentEnrollService.list(queryWrapper);
        if(CollectionUtils.isEmpty(eduStudentEnrolls)){
            throw new AppException("未找到可绑定摇号的已报名学生信息，请退出重新报名");
        }
        int i=1;
        String bindIdcard="";
        for(EduStudentEnroll eduStudentEnroll:eduStudentEnrolls){
            if(i==1){
                bindIdcard=eduStudentEnroll.getIdcard();
                if(!BoolEnum.TRUE.getType().equalsIgnoreCase(eduStudentEnroll.getBindFlag())||!BoolEnum.TRUE.getType().equalsIgnoreCase(eduStudentEnroll.getBindMianFlag())){
                    eduStudentEnroll.setBindFlag(BoolEnum.TRUE.getType());
                    eduStudentEnroll.setBindMianFlag(BoolEnum.TRUE.getType());
                    eduStudentEnroll.setBindIdcard(bindIdcard);
                    eduStudentEnrollService.updateById(eduStudentEnroll);
                }
            }else{
                if(!BoolEnum.TRUE.getType().equalsIgnoreCase(eduStudentEnroll.getBindFlag())||BoolEnum.TRUE.getType().equalsIgnoreCase(eduStudentEnroll.getBindMianFlag())){
                    eduStudentEnroll.setBindFlag(BoolEnum.TRUE.getType());
                    eduStudentEnroll.setBindMianFlag(BoolEnum.FALSE.getType());
                    eduStudentEnroll.setBindIdcard(bindIdcard);
                    eduStudentEnrollService.updateById(eduStudentEnroll);
                }
            }
            i=i+1;
        }
        studentEnroll.setBindIdcard(bindIdcard);
        studentEnroll.setBindMianFlag(BoolEnum.FALSE.getType());
    }
    public void checkSave(SaveStudentEnrollVO vo) {
        if(vo.getLrFlag()==null){
            vo.setLrFlag(false);
        }
        vo.setWhiteFlag(BoolEnum.FALSE.getType());
        if(!vo.getLrFlag()&&StringUtils.isBlank(vo.getStudentEnrollPreId())){
            throw new AppException("预采集ID不能为空");
        }
        //校验基本信息
        if(StringUtils.isBlank(vo.getAddress())){
            throw new AppException("请填先写现居住地");
        }
        if(StringUtils.isBlank(vo.getHouseholdPlace())){
            throw new AppException("请填先写户籍地");
        }

        if(StringUtils.isBlank(vo.getIdcard())){
            throw new AppException("学生证件号码不能为空");
        }
        if(StringUtils.isBlank(vo.getFullName())){
            throw new AppException("学生姓名不能为空");
        }
        if(StringUtils.isBlank(vo.getAddress())){
            throw new AppException("学生现居住地不能为空");
        }
        if(StringUtils.isBlank(vo.getHouseholdPlace())){
            throw new AppException("学生户籍地不能为空");
        }
        vo.setIdcard(vo.getIdcard().trim().toUpperCase());
        vo.setFullName(vo.getFullName().trim());
        if(StringUtils.isBlank(vo.getCardType())){
            vo.setCardType(CardTypeEnum.idcard.getType());
        }
        if(CardTypeEnum.idcard.getType().equals(vo.getCardType())&&StringUtils.isNotBlank(vo.getIdcard())){
            if(!IdcardUtil.isValidCard(vo.getIdcard())){
                throw new AppException("学生身份证格式有误");
            }else{
                if (StringUtils.isBlank(vo.getBirthYmd())) {
                    vo.setBirthYmd(IdcardUtil.getBirth(vo.getIdcard()));
                }
                if (StringUtils.isBlank(vo.getSex())) {
                    int gender = IdcardUtil.getGenderByIdCard(vo.getIdcard());
                    if (gender == 1) {
                        // 男
                        vo.setSex("1");
                    } else {
                        // 女
                        vo.setSex("2");
                    }
                }
            }
        }
        //如果不是身份证类型，出生日期和性别就必须填写
        if(!CardTypeEnum.idcard.getType().equals(vo.getCardType())){
            if(StringUtils.isBlank(vo.getSex())){
                throw new AppException("学生性别不能为空");
            }
            if(StringUtils.isBlank(vo.getBirthYmd())){
                throw new AppException("学生出生日期不能为空");
            }
        }
        boolean flag=true;
        if(StringUtils.isNotBlank(vo.getFatherIdcard())){
            if(StringUtils.isBlank(vo.getFatherCardType())){
                vo.setFatherCardType(CardTypeEnum.idcard.getType());
            }
            vo.setFatherIdcard(vo.getFatherIdcard().trim().toUpperCase());
            if(CardTypeEnum.idcard.getType().equals(vo.getFatherCardType())&&!IdcardUtil.isValidCard(vo.getFatherIdcard())){
                throw new AppException("父亲（监护人）身份证格式有误");
            }
            if(StringUtils.isBlank(vo.getFatherName())){
                throw new AppException("父亲（监护人）姓名不能为空");
            }
            vo.setFatherName(vo.getFatherName().trim());
            if(StringUtils.isBlank(vo.getFatherCellphone())){
                throw new AppException("父亲（监护人）联系电话不能为空");
            }
            flag=false;
        }
        if(StringUtils.isNotBlank(vo.getMotherIdcard())){
            if(StringUtils.isBlank(vo.getMotherCardType())){
                vo.setMotherCardType(CardTypeEnum.idcard.getType());
            }
            if(CardTypeEnum.idcard.getType().equals(vo.getMotherCardType())&&!IdcardUtil.isValidCard(vo.getMotherIdcard())){
                throw new AppException("母亲身份证格式有误");
            }
            vo.setMotherIdcard(vo.getMotherIdcard().trim().toUpperCase());
            if(StringUtils.isBlank(vo.getMotherName())){
                throw new AppException("母亲姓名不能为空");
            }
            vo.setMotherName(vo.getMotherName().trim());
            if(StringUtils.isBlank(vo.getMotherCellphone())){
                throw new AppException("母亲联系电话不能为空");
            }
            flag=false;
        }
        if (flag){
            throw new AppException("请先填写父亲（监护人）或母亲信息");
        }
        if(StringUtils.isBlank(vo.getSchoolId())){
            throw new AppException("请先选择报名学校");
        }

        //学生是否已报名

        Integer year=vo.getYear();
        if (year==null){
            year=eduHelper.thisYear();
            vo.setYear(year);
        }
        //获取学校信息
        EduSchoolVO eduSchool=eduSchoolService.getDetail(vo.getSchoolId());

        if(eduSchool==null){
            throw new AppException("报名的学校不存在或者不招生，如果疑问，请拨打学校电话了解详细情况");
        }
        vo.setAreaCode(eduSchool.getAreaCode());
        vo.setSchoolNum(eduSchool.getSchoolNum());
        vo.setSchoolNature(eduSchool.getSchoolNature());
        vo.setSchoolName(eduSchool.getSchoolName());
        vo.setSchoolCategory(eduSchool.getSchoolCategory());
        vo.setStreet(eduSchool.getStreet());
        List<EduSchoolConfigVO> configList=eduSchool.getConfigList();
        List<EduFormEnrollVO> formEnrollVOList=eduSchool.getFormList();
        //校验学校是否在招生中
        if(!eduSchool.getHasEnrollFlag()){
            if(eduSchool.getSchoolNature().equalsIgnoreCase(SchoolNatureEnum.gb.getType())){
                throw new AppException(year+"年度报名公办学校招生方案不存在，如有疑问，请拨打学校电话了解详细情况");
            }else{
                throw new AppException(year+"年度报名学校招生方案不存在，如有疑问，请拨打学校电话了解详细情况");
            }

        }
        if(CollectionUtils.isEmpty(configList)){
            if(eduSchool.getSchoolNature().equalsIgnoreCase(SchoolNatureEnum.gb.getType())){
                throw new AppException(year + "年度报名公办学校未配置招生计划，如有疑问，请拨打学校电话了解详细情况");
            }else{
                throw new AppException(year + "年度报名学校未配置招生计划，如有疑问，请拨打学校电话了解详细情况");
            }

        }
        EduSchoolConfigVO schoolConfig =null;
        if(!CollectionUtils.isEmpty(configList)) {
            for (EduSchoolConfigVO eduSchoolConfigVO : configList) {
                if (!SchoolNatureEnum.gb.getType().equalsIgnoreCase(eduSchool.getSchoolNature())) {
                    if (eduSchoolConfigVO.getConfigTypeName().equalsIgnoreCase(vo.getConfigTypeName())) {
                        schoolConfig = eduSchoolConfigVO;
                        break;
                    }
                } else {
                    schoolConfig = eduSchoolConfigVO;
                }

            }
            if (schoolConfig == null) {
                if (eduSchool.getSchoolNature().equalsIgnoreCase(SchoolNatureEnum.gb.getType())) {
                    throw new AppException(year + "年度报名公办学校未配置招生计划，如有疑问，请拨打学校电话了解详细情况");
                } else {
                    throw new AppException(year + "年度报名学校未配置招生计划，如有疑问，请拨打学校电话了解详细情况");
                }
            }
        }
        //判断是否在年度招生时间中
        EduOperationCacheQueryVO queryVO=new EduOperationCacheQueryVO();
        queryVO.setClassify("2");
        queryVO.setAreaCode(vo.getAreaCode());
        queryVO.setYear(year);
        EduOperationCacheVO eduOperationCacheVO=eduSystemConfigIntegration.getEduOperation(queryVO);
        if(eduOperationCacheVO==null){
            throw new AppException("本年度义务教育阶段招生参数未配置，请联系系统管理员");
        }
        Map<String, EduOperationCacheDetailVO> operation=eduOperationCacheVO.getOperation();
        EduOperationCacheDetailVO bmOperation=operation.get("YW002");//报名时间
        EduOperationCacheDetailVO bbOperation=operation.get("YW022");//补报时间
        EduOperationCacheDetailVO qbOperation=operation.get("YW010");//抢报时间
        //判断是否报名业务期
        boolean bmFlag = bmOperation==null?false:bmOperation.isFlag();
        //判断是否补报业务期
        boolean bbFlag = bbOperation==null?false:bbOperation.isFlag();
        //判断是否空额抢报业务期
        boolean qbFlag = qbOperation==null?false:qbOperation.isFlag();
        EduOperationCacheDetailVO xxOperation=operation.get("YW023");//学校录入时间

        if(vo.getLrFlag()){
            //学校线下补录，也需要校验年龄信息
            String birthday = "";
            if (vo.getBirthYmd().length() >= 8) {
                //如果格式不一致，转换格式
                birthday = vo.getBirthYmd().replaceAll("-", "").replaceAll("/", "");
            } else if (vo.getBirthYmd().length() == 6) {
                birthday = vo.getBirthYmd() + "01";
            }else{
                throw new AppException("出生日期格式不正确");
            }
            //处理为yyyyMMdd格式
            Integer birthYmd= Integer.parseInt(birthday);
            //年龄不能小于当年最低年龄
            if (schoolConfig.getEndAge() != null) {
                //日期格式转换，转换为yyyyMMdd类型，然后再转为Integer类型进行比对
                Integer endBirthday =
                        Integer.parseInt(
                                DatePattern.PURE_DATE_FORMAT.format(schoolConfig.getEndAge()));
                if(birthYmd>endBirthday){
                    throw new AppException("年龄不符，如有疑问，请拨打学校电话了解详细情况");
                }
            }

            if (StringUtils.isBlank(vo.getRemarks())) {
                throw new AppException("请输入录取依据");
            }
            if (StringUtils.isBlank(vo.getParam20())) {
                throw new AppException("必须传入佐证资材料");
            }
            //判断是否在黑名单
            //检验是否在黑名单中，黑名单中不能线下录入
            QueryWrapper<EduStudentEnrollBlacklist> blacklistQueryWrapper = new QueryWrapper<>();
            blacklistQueryWrapper.lambda().eq(EduStudentEnrollBlacklist::getIdcard, vo.getIdcard())
                    .eq(EduStudentEnrollBlacklist::getYear, vo.getYear())
                    .eq(EduStudentEnrollBlacklist::getStatus, BoolEnum.TRUE.getType())
                    .eq(EduStudentEnrollBlacklist::getAreaCode, eduSchool.getAreaCode());
            if (eduStudentEnrollBlacklistService.count(blacklistQueryWrapper) > 0) {
                throw new AppException("该学生申请过退出东阳报名，不能重新录入");
            }
            //判断是否公办中系统无效，存在无效数据不能录入
            QueryWrapper<EduStudentEnroll> queryWrapperWx = new QueryWrapper<>();
            queryWrapperWx.lambda().eq(EduStudentEnroll::getIdcard,vo.getIdcard())
                    .eq(EduStudentEnroll::getYear,vo.getYear())
                    .eq(EduStudentEnroll::getEnrollStatus,EnrollStatusFormalEnum.xtblq.getType())
                    .eq(EduStudentEnroll::getSchoolNature,SchoolNatureEnum.gb.getType());
            if (eduStudentEnrollService.count(queryWrapperWx)>0){
                throw new AppException("该学生存在公办学校报名无效数据，请联系基教科处理后才能录入");
            }
            //学校录入
            vo.setSource("6");
            if(xxOperation==null){
                throw new AppException(year+"年度义务段学校线下报名录入工作尚未开始");
            }
            if(!xxOperation.isFlag()){
                if ("1".equalsIgnoreCase(xxOperation.getType()) || "3".equalsIgnoreCase(xxOperation.getType())) {
                    throw new AppException(year+"年度义务段学校线下报名录入工作尚未开始");
                }else{
                    throw new AppException(year+"年度义务段学校线下报名录入工作已结束");
                }
            }
            vo.setBbFlag(BoolEnum.FALSE.getType());
            vo.setSpareRegisterFlag(BoolEnum.FALSE.getType());
            EduStudentEnroll enroll = null;
            List<String> enrollStatusList=new ArrayList<>();
            enrollStatusList.add(EnrollStatusFormalEnum.dsh.getType());
            enrollStatusList.add(EnrollStatusFormalEnum.dlq.getType());
            enrollStatusList.add(EnrollStatusFormalEnum.lq.getType());
            QueryWrapper<EduStudentEnroll> otherQueryWrapper = new QueryWrapper<>();
            otherQueryWrapper.lambda().ne(EduStudentEnroll::getSchoolId, vo.getSchoolId())
                    .eq(EduStudentEnroll::getYear, vo.getYear())
                    .in(EduStudentEnroll::getEnrollStatus,enrollStatusList)
                    .eq(EduStudentEnroll::getIdcard, vo.getIdcard());
            if (eduStudentEnrollService.count(otherQueryWrapper) > 0) {
                throw new AppException("该学生已在市内其他学校待审核/待录取/录取，不能直接录入，如有疑问，请联系基教科核对");
            }
            if (StringUtils.isNotBlank(vo.getStudentEnrollId())) {
                enroll = eduStudentEnrollService.getById(vo.getStudentEnrollId());
                if (EnrollStatusFormalEnum.lq.getType().equals(enroll.getEnrollStatus())) {
                    throw new AppException("该学生已是录取状态，请核对");
                }
                if (EnrollStatusFormalEnum.ywblq.getType().equals(enroll.getEnrollStatus())) {
                    throw new AppException("该学生业务操作为不予录取，如需重新录入，请联系基教科处理数据");
                }
                if (SchoolNatureEnum.gb.getType().equals(vo.getSchoolNature())) {
                    if (!enroll.getConfigName().equalsIgnoreCase(vo.getConfigName())) {
                        vo.setApplyId(null);
                    }
                }else{
                    if (!enroll.getConfigTypeName().equalsIgnoreCase(vo.getConfigTypeName())) {
                        vo.setApplyId(null);
                    }
                }
            } else {
                QueryWrapper<EduStudentEnroll> enrollQueryWrapper = new QueryWrapper<>();
                enrollQueryWrapper.lambda().select(EduStudentEnroll::getEnrollStatus,EduStudentEnroll::getConfigName,EduStudentEnroll::getConfigTypeName,EduStudentEnroll::getSchoolNature,EduStudentEnroll::getSchoolId,EduStudentEnroll::getSchoolCategory).eq(EduStudentEnroll::getSchoolId, vo.getSchoolId())
                        .eq(EduStudentEnroll::getYear, vo.getYear())
                        .eq(EduStudentEnroll::getIdcard, vo.getIdcard());
                List<EduStudentEnroll> eduStudentEnrollList = eduStudentEnrollService.list(enrollQueryWrapper);
                if (!CollectionUtils.isEmpty(eduStudentEnrollList)) {
                    enroll = eduStudentEnrollList.get(0);
                    for(EduStudentEnroll oldEnroll:eduStudentEnrollList){
                        if (EnrollStatusFormalEnum.lq.getType().equals(oldEnroll.getEnrollStatus())) {
                            throw new AppException("该学生已是录取状态，请核对");
                        }
                        if (EnrollStatusFormalEnum.ywblq.getType().equals(oldEnroll.getEnrollStatus())) {
                            throw new AppException("该学生业务操作为不予录取，如需重新录入，请联系基教科处理数据");
                        }
                    }
                    vo.setStudentEnrollId(enroll.getStudentEnrollId());
                    if (SchoolNatureEnum.gb.getType().equals(vo.getSchoolNature())) {
                        if (!enroll.getConfigName().equalsIgnoreCase(vo.getConfigName())) {
                            vo.setApplyId(null);
                        }
                    }else{
                        if (!enroll.getConfigTypeName().equalsIgnoreCase(vo.getConfigTypeName())) {
                            vo.setApplyId(null);
                        }
                    }
                } else {
                    if (SchoolNatureEnum.gb.getType().equals(vo.getSchoolNature())) {
                        if(StringUtils.isBlank(vo.getConfigTypeName())){
                            vo.setConfigTypeName("公办");
                        }
                    }
                    vo.setApplyId(null);
                }
            }
            //线下录入人数判断
            //判断学校录入指标管理是否有配置存在
            QueryWrapper<EduSchoolLr> lrQueryWrapper = new QueryWrapper<>();
            lrQueryWrapper.lambda()
                    .eq(EduSchoolLr::getYear,vo.getYear())
                    .eq(EduSchoolLr::getSchoolId,vo.getSchoolId());
            EduSchoolLr schoolLr = eduSchoolLrService.getOne(lrQueryWrapper, false);
            if (ObjectUtils.isEmpty(schoolLr)){
                throw new AppException("未配置学校线下录入指标，请联系教育局进行配置");
            }
            if (schoolLr.getLrCount() == 0){
                throw new AppException(schoolLr.getSchoolName()+"的可录入人数为0，请联系教育局进行配置！");
            }
            long enrollStudentCount = eduStudentEnrollService.count(Wrappers.<EduStudentEnroll>lambdaQuery()
                    .eq(EduStudentEnroll::getYear, vo.getYear())
                    .eq(EduStudentEnroll::getEnrollStatus, EnrollStatusFormalEnum.lq.getType())
                    .eq(EduStudentEnroll::getSchoolId, vo.getSchoolId())
                    .eq(EduStudentEnroll::getSource, "6"));
            if (enrollStudentCount >= schoolLr.getLrCount()){
                throw new AppException("学校线下录入的人数大于系统配置人数指标，无法进行学生录入，请联系教育局进行配置");
            }
            //查询招生方案
            QueryWrapper<EduEnrollmenBrochure> brochureQueryWrapper = new QueryWrapper<>();
            brochureQueryWrapper.lambda().eq(EduEnrollmenBrochure::getYear, vo.getYear())
                    .eq(EduEnrollmenBrochure::getSchoolId, vo.getSchoolId());
            EduEnrollmenBrochure eduEnrollmenBrochure = eduEnrollmenBrochureService.getOne(brochureQueryWrapper, false);
            if (eduEnrollmenBrochure == null) {
                throw new AppException("未找到招生方案信息");
            }
            //超过计划数不能录取
            //计划人数
            Integer enrolment = schoolConfig.getEnrolment() == null ? 0 : schoolConfig.getEnrolment();

            //已录取人数
            QueryWrapper<EduStudentEnroll> lqQuery = new QueryWrapper<>();
            lqQuery.lambda().eq(EduStudentEnroll::getEnrollStatus, EnrollStatusFormalEnum.lq.getType())
                    .eq(EduStudentEnroll::getYear, vo.getYear())
                    .eq(EduStudentEnroll::getAdmitFlag, BoolEnum.TRUE.getType())
                    .eq(EduStudentEnroll::getSchoolId, vo.getSchoolId());
            if ("1".equals(vo.getSchoolNature()) || "2".equals(vo.getSchoolNature())) {
                //民办
                lqQuery.lambda().eq(EduStudentEnroll::getConfigTypeName, vo.getConfigTypeName());
            }
            Long lqCount = eduStudentEnrollService.count(lqQuery);
            if (lqCount >= enrolment) {
                throw new AppException("录取人数已到达计划人数，如还需录取，请联系基教科修改招生计划");
            }
            // 调用金华接口判断是否已录取
            CommonVO commonVO=new CommonVO();
            commonVO.setAreaCode(vo.getAreaCode());
            commonVO.setParamKey("localLqDw");
            String localDw = commonIntegration.getValueByKeyAndArea(commonVO);
            if(StringUtils.isBlank(localDw)){
                throw new AppException("请先配置金华教育本地录取单位");
            }
            if (enroll != null) {
                EduJhjyQuerySyncVO eduJhjyQuerySyncVO = eduJhjyQuerySyncIntegration.queryJhjy(enroll);
            if (StringUtils.isNotBlank(eduJhjyQuerySyncVO.getLqDw())&&!localDw.equals(eduJhjyQuerySyncVO.getLqDw())) {
                throw new AppException("该学生已被" + eduJhjyQuerySyncVO.getLqDw() + "录取，请联系基教科处理");
            }
            }
        }else {
            boolean whiteFlag=false;
            LambdaQueryWrapper<EduStudentEnrollWhite> whiteLambdaQueryWrapper = new LambdaQueryWrapper<>();
            whiteLambdaQueryWrapper.eq(EduStudentEnrollWhite::getIdcard, vo.getIdcard())
                    .eq(EduStudentEnrollWhite::getYear, vo.getYear())
                    .eq(EduStudentEnrollWhite::getAuthStatus,BoolEnum.TRUE.getType())
                    .eq(EduStudentEnrollWhite::getSchoolId, vo.getSchoolId());
            if (eduStudentEnrollWhiteService.count(whiteLambdaQueryWrapper) <= 0) {
                whiteFlag=false;
            }else{
                whiteFlag=true;
                vo.setWhiteFlag(BoolEnum.TRUE.getType());
            }
            EduStudentEnrollPreKeyVO keyVO=new EduStudentEnrollPreKeyVO();
            keyVO.setStudentEnrollPreId(vo.getStudentEnrollPreId());
            EduStudentEnrollPreVO eduStudentEnrollPre=eduStudentEnrollIntegration.getBeforeByPreId(keyVO);
            if(eduStudentEnrollPre==null){
                throw new AppException("未找到新生登记信息");
            }

            //白名单就可以报名
            if(whiteFlag){
                eduStudentEnrollPre.setCanEnroll(BoolEnum.TRUE.getType());
                eduStudentEnrollPre.setCanEnrollFlag(true);
                eduStudentEnrollPre.setFmJzzFlag(true);
            }else{
                if (!eduStudentEnrollPre.getCanEnrollFlag()&&!"直升".equalsIgnoreCase(vo.getConfigTypeName())) {
                    throw new AppException("您或您的孩子目前没有取得东阳市公安局核发的处于有效期内的《浙江省居住证》，不符合东阳义务段学校新生报名最低条件。");
                }
            }
            vo.setYear(eduStudentEnrollPre.getYear());
            //判断是否有待审核、待录取以及已录取数据，有的话，不能报名
            Boolean enrollFlag= eduStudentEnrollIntegration.checkEnroll(eduStudentEnrollPre.getIdcard(),eduStudentEnrollPre.getYear());
            if(enrollFlag){
                throw new AppException("已报名，请前往我的报名查看报名记录");
            }
            year=vo.getYear();
            if (bmOperation == null) {
                throw new AppException(year + "年度义务段学校招生报名工作尚未开始");
            }
            if (!bmFlag) {
                if ("1".equalsIgnoreCase(bmOperation.getType()) || "3".equalsIgnoreCase(bmOperation.getType())) {
                    throw new AppException(year + "年度义务段学校招生报名工作尚未开始");
                } else {
                    if (!bbFlag) {
                        if ("1".equalsIgnoreCase(bbOperation.getType())) {
                            //补报未开始
                            throw new AppException(year + "年度义务段学校招生补报工作尚未开始");
                        } else if ("3".equalsIgnoreCase(bbOperation.getType())) {
                            throw new AppException(year + "年度义务段学校招生报名工作已结束");
                        } else {
                            if (!qbFlag) {
                                if ("1".equalsIgnoreCase(qbOperation.getType())) {
                                    //抢报未开始
                                    throw new AppException(year + "年度义务段学校招生空额补报工作尚未开始");
                                } else {
                                    throw new AppException(year + "年度义务段学校招生报名工作已结束");
                                }
                            }
                        }
                    }
                }
            }

            //补报
            if (bbFlag) {
                vo.setBbFlag(BoolEnum.TRUE.getType());
                // 补报是否需要之前有报名记录
                CommonVO commonVO=new CommonVO();
                commonVO.setAreaCode(vo.getAreaCode());
                commonVO.setParamKey("edu_bb_check_enroll");
                String bbCheckEnrollStr=commonIntegration.getValueByKeyAndArea(commonVO);
                if ("1".equals(bbCheckEnrollStr)||"true".equals(bbCheckEnrollStr)){
                    if(!eduStudentEnrollIntegration.hasEnroll(vo.getIdcard(),vo.getYear())){
                        throw new AppException("本次补报只允许之前存在有效报名记录的人才能报名,未报名过的人员请关注东阳教育微信公众号了解最新情况");
                    }
                }
            } else {
                vo.setBbFlag(BoolEnum.FALSE.getType());
            }

            //空额抢报
            if (qbFlag) {
                vo.setSpareRegisterFlag(BoolEnum.TRUE.getType());
            } else {
                vo.setSpareRegisterFlag(BoolEnum.FALSE.getType());
            }
            if(!qbFlag){
                if("0".equals(vo.getSchoolNature())){
                    if(StringUtils.isBlank(vo.getConfigName())){
                        throw new AppException("请先选择报名类别");
                    }
                    if(StringUtils.isBlank(vo.getConfigTypeName())){
                        throw new AppException("报名大类不能为空");
                    }
                    if(StringUtils.isBlank(vo.getFormId())){
                        throw new AppException("报名表单id不能为空");
                    }
                }else{
                    if(StringUtils.isBlank(vo.getConfigTypeName())){
                        throw new AppException("报名类别不能为空");
                    }
                }

            }else{
                if(StringUtils.isBlank(vo.getConfigName())){
                    vo.setConfigName("空额补报");
                }
                //判断空额抢报是否报满
                QueryWrapper<EduSchoolVacancy> vacancyQueryWrapper=new QueryWrapper<>();
                vacancyQueryWrapper.lambda().eq(EduSchoolVacancy::getYear,year)
                        .eq(EduSchoolVacancy::getAuthStatus, AuthStatusEnum.YES.getType())
                        .ge(EduSchoolVacancy::getVacancyNum,0)
                        .eq(EduSchoolVacancy::getSchoolId,vo.getSchoolId());
                List<EduSchoolVacancy> vacancyList=eduSchoolVacancyService.list(vacancyQueryWrapper);
                if(CollectionUtils.isEmpty(vacancyList)){
                    throw new AppException("补报名额已报满，如有疑问，请致电学校");
                }
                EduSchoolVacancy eduSchoolVacancy=vacancyList.get(0);
                //查询已抢报人数
                QueryWrapper<EduStudentEnroll> qbQuery=new QueryWrapper<>();
                List<String> enrollStatusList=new ArrayList<>();
                enrollStatusList.add(EnrollStatusFormalEnum.dsh.getType());
                enrollStatusList.add(EnrollStatusFormalEnum.dlq.getType());
                enrollStatusList.add(EnrollStatusFormalEnum.lq.getType());
                qbQuery.lambda().eq(EduStudentEnroll::getSchoolId,vo.getSchoolId())
                        .eq(EduStudentEnroll::getYear,year)
                        .in(EduStudentEnroll::getEnrollStatus,enrollStatusList)
                        .eq(EduStudentEnroll::getSpareRegisterFlag,BoolEnum.TRUE.getType());
                long qbCount=eduStudentEnrollService.count(qbQuery);
                if(qbCount>=eduSchoolVacancy.getVacancyNum()){
                    throw new AppException("补报名额已报满，如有疑问，请致电学校");
                }
            }



            if(!vo.getSchoolCategory().equals(eduSchool.getSchoolCategory())){
                throw new AppException("报名的学段和学校的类型不一致");
            }
            vo.setSchoolNature(eduSchool.getSchoolNature());
            //不是补报和抢报，第一个一定要是公办学校
            if (!bbFlag && !qbFlag && !"0".equals(eduSchool.getSchoolNature()) && !vo.getZsbEnrollFlag()) {
                throw new AppException("选择的公办学校性质不匹配，如果疑问，请拨打学校电话了解详细情况");
            }
            vo.setSchoolNum(eduSchool.getSchoolNum());
            vo.setSchoolName(eduSchool.getSchoolName());
            vo.setStreet(eduSchool.getStreet());
            if (BoolEnum.TRUE.getType().equals(vo.getSpecialFlag())) {
                EduFormSpecial eduFormSpecial = eduFormSpecialService.getById(vo.getFormId());
                if (eduFormSpecial == null) {
                    throw new AppException("未匹配到特殊通道配置的报名表单，如果疑问，请拨打学校电话了解详细情况");
                }
                if (!eduFormSpecial.getStatus().equals(BoolEnum.TRUE.getType())) {
                    throw new AppException("特殊通道配置报名表单已无效，如果疑问，请拨打学校电话了解详细情况");
                }
                //特殊通道排序号用99
                vo.setFormOrderNo(99);
                //特殊房产类 需校验房产
                if (BoolEnum.TRUE.getType().equalsIgnoreCase(eduFormSpecial.getHouseFlag())) {
                    if (StringUtils.isBlank(vo.getHouseId())) {
                        throw new AppException("特殊通道中特殊房产的报名必须绑定系统中的房产数据才能报名");
                    }
                }
            } else {
                if (!qbFlag) {

                        if ("0".equals(eduSchool.getSchoolNature())) {
                            //公办学校
                            if (CollectionUtils.isEmpty(formEnrollVOList)) {
                                throw new AppException("未匹配到学校配置的报名表单，如果疑问，请拨打学校电话了解详细情况");
                            }
                            EduFormEnrollVO eduForm = null;
                            for (EduFormEnrollVO eduFormEnrollVO : formEnrollVOList) {
                                if (eduFormEnrollVO.getFormId().equalsIgnoreCase(vo.getFormId())) {
                                    eduForm = eduFormEnrollVO;
                                    break;
                                }
                            }
                            if (eduForm == null) {
                                throw new AppException("未匹配到学校配置的报名表单，如果疑问，请拨打学校电话了解详细情况");
                            }
                            if (BoolEnum.FALSE.getType().equalsIgnoreCase(eduForm.getValidStatus())) {
                                throw new AppException("学校配置报名表单已无效，如果疑问，请拨打学校电话了解详细情况");
                            }
                            if (eduForm.getOrderNo() == null) {
                                throw new AppException("学校配置报名类别排序号为空，如果疑问，请拨打学校电话了解详细情况");
                            }
                            vo.setFormOrderNo(eduForm.getOrderNo());
                        } else {
                            //民办或民转公
                            if (StringUtils.isBlank(vo.getConfigTypeName())) {
                                throw new AppException(eduSchool.getSchoolName() + "的报名类别不能为空");
                            }
                            if (schoolConfig.getOrderNo() == null) {
                                throw new AppException(year + "年度报名学校招生计划设置有误，如有疑问，请拨打学校电话了解详细情况");
                            }
                            if (schoolConfig.getOrderNo() == null) {
                                throw new AppException("学校配置报名类别排序号为空，如果疑问，请拨打学校电话了解详细情况");
                            }
                            vo.setFormOrderNo(schoolConfig.getOrderNo());
                        }

                } else {
                    //公办抢报排序号用98
                    if ("0".equals(eduSchool.getSchoolNature())) {
                        vo.setConfigTypeName("公办");
                        vo.setFormOrderNo(98);
                    } else {
                        if (StringUtils.isBlank(vo.getConfigTypeName())) {
                            throw new AppException(eduSchool.getSchoolName() + "的报名类别不能为空");
                        }
                        if (schoolConfig.getOrderNo() == null) {
                            throw new AppException(year + "年度报名学校招生计划设置有误，如有疑问，请拨打学校电话了解详细情况");
                        }
                        vo.setFormOrderNo(schoolConfig.getOrderNo());
                    }
                }
            }

            //判断年龄是否符合，特殊标识人员不处理
            if(!BoolEnum.TRUE.getType().equals(vo.getSpecialFlag())) {
                if(StringUtils.isBlank(vo.getSource())){
                    vo.setSource("1");
                }

                if (StringUtils.isBlank(vo.getBirthYmd())) {
                    throw new AppException("出生日期不能为空");
                }
                String birthday = "";
                if (vo.getBirthYmd().length() >= 8) {
                    birthday = vo.getBirthYmd().replaceAll("-", "").replaceAll("/", "");
                } else if (vo.getBirthYmd().length() == 6) {
                    birthday = vo.getBirthYmd() + "01";
                }else{
                    throw new AppException("出生日期格式不正确");
                }
                Integer birthYmd= Integer.parseInt(birthday);
                if (schoolConfig.getStartAge() != null) {
                    Integer startBirthday =
                            Integer.parseInt(
                                    DatePattern.PURE_DATE_FORMAT.format(schoolConfig.getStartAge()));
                    if(birthYmd<startBirthday){
                        throw new AppException("年龄不符，如有疑问，请拨打学校电话了解详细情况");
                    }
                }
                if (schoolConfig.getEndAge() != null) {
                    Integer endBirthday =
                            Integer.parseInt(
                                    DatePattern.PURE_DATE_FORMAT.format(schoolConfig.getEndAge()));
                    if(birthYmd>endBirthday){
                        throw new AppException("年龄不符，如有疑问，请拨打学校电话了解详细情况");
                    }
                }

            }else{
                if(StringUtils.isBlank(vo.getSource())){
                    vo.setSource("5");
                }
                //判断是否学校特殊人员
                List<EduStudentEnrollBeforeVO> beforeList=eduStudentEnrollPre.getEnrollBeforeList();
                if(CollectionUtils.isEmpty(beforeList)){
                    throw new AppException("不能报该学校特殊通道，如有疑问，请拨打学校电话了解详细情况");
                }else{
                    for(EduStudentEnrollBeforeVO beforeVO:beforeList){
                        if (beforeVO.getEnrollBeforeId().equals(vo.getEnrollBeforeId())) {
                            if (BoolEnum.TRUE.getType().equalsIgnoreCase(beforeVO.getSpecialFlag())) {
                                vo.setConfigTypeName(beforeVO.getConfigTypeName());
                                if (!vo.getConfigName().equals(beforeVO.getConfigName())) {
                                    throw new AppException("报名的特殊通道类别与学校录入的不一致，如有疑问，请拨打学校电话了解详细情况");
                                }
                                vo.setConfigName(beforeVO.getConfigName());
                                vo.setHouseId(beforeVO.getHouseId());
                                vo.setHouseholdId(beforeVO.getHouseholdId());
                                break;
                            } else {
                                throw new AppException("数据异常，该类别不是特殊通道类别，请重新登录后重试");
                            }
                        } else {
                            if (BoolEnum.TRUE.getType().equalsIgnoreCase(beforeVO.getSpecialFlag())
                                    && beforeVO.getSchoolId().equalsIgnoreCase(vo.getSchoolId())) {
                                vo.setConfigTypeName(beforeVO.getConfigTypeName());
                                if (!vo.getConfigName().equals(beforeVO.getConfigName())) {
                                    throw new AppException("报名的特殊通道类别与学校录入的不一致，如有疑问，请拨打学校电话了解详细情况");
                                }
                                vo.setConfigName(beforeVO.getConfigName());
                                vo.setHouseId(beforeVO.getHouseId());
                                vo.setHouseholdId(beforeVO.getHouseholdId());
                                break;
                            }
                        }

                    }

                }

            }
            if(bbFlag){
                //补报只能在补报名单范围内
                QueryWrapper<EduSchoolBb> bbQueryWrapper=new QueryWrapper<>();
                bbQueryWrapper.lambda().eq(EduSchoolBb::getYear,year)
                        .eq(EduSchoolBb::getSchoolId,vo.getSchoolId());
                List<EduSchoolBb> eduSchoolBbList=eduSchoolBbService.list(bbQueryWrapper);
                if(CollectionUtils.isEmpty(eduSchoolBbList)){
                    throw new AppException("您报名的学校不在可补报学校名单范围内，请选择其他学校报名");
                }
                EduSchoolBb eduSchoolBb=eduSchoolBbList.get(0);
                if(BoolEnum.TRUE.getType().equals(eduSchoolBb.getFullFlag())){
                    //报满即止
                    checkFull(vo,schoolConfig);
                }
            }
            //是否调配
            vo.setDeployFlag(BoolEnum.FALSE.getType());
            //现场核验
            vo.setIcertificatioFlag(BoolEnum.FALSE.getType());
            //判断是第一次报名的时候是否同时报名民办或民转公
            if(!bbFlag&&!qbFlag&&!CollectionUtils.isEmpty(vo.getOtherSchoolList())) {
                boolean mbDyzy=false;
                boolean mbDezy=false;
                boolean mbDszy = false;
                for (ApplySchoolVO applySchoolVO : vo.getOtherSchoolList()) {
                    if (StringUtils.isBlank(vo.getSchoolId())) {
                        throw new AppException("请先选择学校");
                    }
                    EduSchoolVO eduSchoolMb = eduSchoolService.getDetail(applySchoolVO.getSchoolId());
                    if (eduSchoolMb == null) {
                        throw new AppException("报名的学校中有不存在或者不招生，如果疑问，请拨打学校电话了解详细情况");
                    } else {
                        applySchoolVO.setSchoolNum(eduSchoolMb.getSchoolNum());
                        applySchoolVO.setSchoolCategory(eduSchoolMb.getSchoolCategory());
                        applySchoolVO.setSchoolNature(eduSchoolMb.getSchoolNature());
                        applySchoolVO.setSchoolName(eduSchoolMb.getSchoolName());
                        applySchoolVO.setStreet(eduSchoolMb.getStreet());
                        applySchoolVO.setAreaCode(eduSchoolMb.getAreaCode());
                    }
                    //校验是否存在招生方案
                    if (!eduSchoolMb.getHasEnrollFlag()) {
                        throw new AppException(year + "年度报名"+eduSchoolMb.getSchoolName()+"招生方案不存在，如有疑问，请拨打学校电话了解详细情况");
                    }

                    if("0".equals(eduSchoolMb.getSchoolNature())){
                        if(StringUtils.isBlank(applySchoolVO.getAspirationType())){
                            applySchoolVO.setAspirationType("1");
                        }
                        if(StringUtils.isBlank(applySchoolVO.getConfigName())){
                            throw new AppException(eduSchoolMb.getSchoolName()+"的报名类别不能为空");
                        }
                        if(StringUtils.isBlank(applySchoolVO.getFormId())){
                            throw new AppException("报名表单id不能为空");
                        }
                        List<EduFormEnrollVO> formList=eduSchoolMb.getFormList();
                        EduFormEnrollVO eduForm=null;
                        for(EduFormEnrollVO formEnrollVO:formList){
                            if(formEnrollVO.getFormId().equalsIgnoreCase(applySchoolVO.getFormId())){
                                eduForm=formEnrollVO;
                                break;
                            }
                        }
                        if(eduForm==null){
                            throw new AppException("未匹配到学校配置的报名表单，如果疑问，请拨打学校电话了解详细情况");
                        }
                        if(BoolEnum.FALSE.getType().equalsIgnoreCase(eduForm.getValidStatus())){
                            throw new AppException("学校配置报名表单已无效，如果疑问，请拨打学校电话了解详细情况");
                        }
                        applySchoolVO.setOrderNo(eduForm.getOrderNo());
                    }else{
                        if ("2".equals(eduSchoolMb.getSchoolNature())) {
                            //全市符合A、B、C、特殊户口、特殊通道
                            boolean canEnrollMzg = false;
                            boolean onlyCFlag=true;
                            boolean cFlag=false;
                            if (!CollectionUtils.isEmpty(eduStudentEnrollPre.getEnrollBeforeList())) {
                                for (EduStudentEnrollBeforeVO beforeVO : eduStudentEnrollPre.getEnrollBeforeList()) {
                                    if ("A".equalsIgnoreCase(beforeVO.getConfigName()) || "B".equalsIgnoreCase(beforeVO.getConfigName())
                                            || "C".equalsIgnoreCase(beforeVO.getConfigName()) || "特殊户口".equalsIgnoreCase(beforeVO.getConfigName())
                                            || BoolEnum.TRUE.getType().equalsIgnoreCase(beforeVO.getSpecialFlag())) {
                                        if ("A".equalsIgnoreCase(beforeVO.getConfigName()) || "B".equalsIgnoreCase(beforeVO.getConfigName())
                                               || "特殊户口".equalsIgnoreCase(beforeVO.getConfigName())) {
                                            onlyCFlag=false;
                                        }
                                        if ("C".equalsIgnoreCase(beforeVO.getConfigName())){
                                            cFlag=true;
                                        }
                                        canEnrollMzg = true;
                                        if (BoolEnum.TRUE.getType().equalsIgnoreCase(beforeVO.getSpecialFlag())) {

                                            if ("缓学复读".equalsIgnoreCase(beforeVO.getConfigName())) {
                                                canEnrollMzg = false;
                                            }
                                            EduFormSpecialQueryVO formSpecialQueryVO = new EduFormSpecialQueryVO();
                                            formSpecialQueryVO.setAreaCode(beforeVO.getAreaCode());
                                            formSpecialQueryVO.setSmallClassValue(beforeVO.getConfigName());
                                            formSpecialQueryVO.setBigClassValue(beforeVO.getConfigTypeName());
                                            formSpecialQueryVO.setYear(beforeVO.getYear());
                                            EduFormSpecialVO eduFormSpecialVO = eduFormSpecialService.getBySmallValue(formSpecialQueryVO);
                                            if (eduFormSpecialVO == null || !BoolEnum.TRUE.getType().equalsIgnoreCase(eduFormSpecialVO.getMzgFlag())) {
                                                canEnrollMzg = false;
                                            }else{
                                                onlyCFlag=false;
                                            }
                                        }
                                    }

                                }
                                if (onlyCFlag&&cFlag){
                                    //判断房产是否12月31号之前
                                    if (!"1".equals(eduSchoolMb.getHouseEndTimeNo())){
                                        EduOperationCacheQueryVO queryOpVO=new EduOperationCacheQueryVO();
                                        queryOpVO.setOperating("YW024");
                                        queryOpVO.setAreaCode(eduSchoolMb.getAreaCode());
                                        queryOpVO.setClassify("2");
                                        EduOperationCacheDetailVO fcOption=eduSystemConfigIntegration.getEduOperationOne(queryOpVO);
                                        if (fcOption!=null&&fcOption.getEndTime()!=null) {
                                            Long houseEndTime=Long.parseLong(DateUtil.format(fcOption.getEndTime(),"yyyyMMdd"));
                                            List<EduHouseVO> houseVOList = eduStudentEnrollPre.getHouseList();
                                            boolean cCanFlag=false;
                                            for (EduHouseVO eduHouseVO : houseVOList) {
                                                if (BoolEnum.TRUE.getType().equals(eduHouseVO.getStatus()) && AuthStatusEnum.YES.getType().equals(eduHouseVO.getAuthStatus()) && !BoolEnum.TRUE.getType().equals(eduHouseVO.getUseFlag())) {
                                                    if (eduHouseVO.getRegisterDate() != null) {
                                                        Long djTime=Long.parseLong(DateUtil.format(eduHouseVO.getRegisterDate(),"yyyyMMdd"));
                                                        if(djTime-houseEndTime<=0){
                                                            cCanFlag=true;
                                                        }
                                                    }
                                                }

                                            }
                                            if (!cCanFlag){
                                                throw new AppException("你的房产登记时间不在有效期内，不能报公参民转公学校");
                                            }
                                        }
                                    }

                                }
                            }

                            if (!canEnrollMzg&&!"直升".equalsIgnoreCase(applySchoolVO.getConfigTypeName())&&!eduStudentEnrollPre.getGatFlag()) {
                                throw new AppException("您不符合东阳“公参民转公过渡期学校”报名条件");
                            }
                        }else if("1".equals(eduSchoolMb.getSchoolNature())){
                            /***民办去除户籍限制
                            if(!"直升".equalsIgnoreCase(applySchoolVO.getConfigTypeName())&&!eduStudentEnrollPre.getGatFlag()){
                                if(eduStudentEnrollPre.getHouseHoldVO()==null){
                                    throw new AppException("您的孩子户籍不在东阳市内，不能报名民办学校");
                                }
                            }***/
                            //有房或者有户或者港澳台是第一批次 其他的是第二批次
                            if(eduStudentEnrollPre.getHouseHoldVO()!=null||!CollectionUtils.isEmpty(eduStudentEnrollPre.getHouseList())||eduStudentEnrollPre.getGatFlag()){
                                applySchoolVO.setConfigNameNum(1);
                            }else{
                                applySchoolVO.setConfigNameNum(2);
                            }
                        }
                        if(StringUtils.isBlank(applySchoolVO.getAspirationType())){
                            applySchoolVO.setAspirationType("1");
                        }
                        if("1".equals(eduSchoolMb.getSchoolNature())){
                            if("1".equals(applySchoolVO.getAspirationType())){
                                mbDyzy=true;
                            }
                            if("2".equals(applySchoolVO.getAspirationType())){
                                mbDezy=true;
                            }
                            if ("3".equals(applySchoolVO.getAspirationType())) {
                                mbDszy = true;
                            }
                        }
                        if(StringUtils.isBlank(vo.getConfigTypeName())){
                            throw new AppException(eduSchoolMb.getSchoolName()+"的报名类别不能为空");
                        }
                        List<EduSchoolConfigVO> mbConfigList=eduSchoolMb.getConfigList();
                        if(CollectionUtils.isEmpty(mbConfigList)){
                            throw new AppException(year + "年度报名学校未配置招生计划，如有疑问，请拨打学校电话了解详细情况");
                        }
                        EduSchoolConfigVO schoolConfigMb =null;
                        for(EduSchoolConfigVO eduSchoolConfigVO:mbConfigList){
                            if(eduSchoolConfigVO.getConfigTypeName().equalsIgnoreCase(applySchoolVO.getConfigTypeName())){
                                schoolConfigMb=eduSchoolConfigVO;
                                break;
                            }
                        }
                        if (schoolConfigMb == null) {
                            throw new AppException(year + "年度报名学校报名类别不匹配，如有疑问，请拨打学校电话了解详细情况");
                        }
                        if (schoolConfigMb.getOrderNo() == null) {
                            throw new AppException(year + "年度报名学校招生计划设置有误，如有疑问，请拨打学校电话了解详细情况");
                        }
                        applySchoolVO.setOrderNo(schoolConfigMb.getOrderNo());
                        //判断是否直升班
                        EduPrivateAppointStudentVO eduPrivateAppointStudentVO=eduStudentEnrollPre.getZsbVO();
                        if (BoolEnum.TRUE.getType().equals(schoolConfigMb.getZsFlag())) {
                            if(StringUtils.isNotBlank(vo.getAbandonZsbFlag())&&BoolEnum.TRUE.getType().equals(vo.getAbandonZsbFlag())){
                                throw new AppException("您已放弃直升，不能报直升类别");
                            }


                            if (eduPrivateAppointStudentVO!=null) {
                                if(eduPrivateAppointStudentVO.getSchoolId().equalsIgnoreCase(applySchoolVO.getSchoolId())
                                    &&BoolEnum.TRUE.getType().equalsIgnoreCase(eduPrivateAppointStudentVO.getStatus())){
                                    vo.setPrivateAppointFlag(BoolEnum.TRUE.getType());
                                    vo.setPrivateAppointStudentId(eduPrivateAppointStudentVO.getPrivateAppointStudentId());
                                }else{
                                    throw new AppException("没有该学校的直升班名额，不能选择该类别，如有疑问，请拨打学校电话了解详细情况");
                                }

                            } else {
                                throw new AppException("没有该学校的直升班名额，不能选择该类别，如有疑问，请拨打学校电话了解详细情况");
                            }
                        } else {

                            if (eduPrivateAppointStudentVO!=null) {
                                if(eduPrivateAppointStudentVO.getSchoolId().equalsIgnoreCase(applySchoolVO.getSchoolId())
                                        &&BoolEnum.TRUE.getType().equalsIgnoreCase(eduPrivateAppointStudentVO.getStatus())){
                                    vo.setPrivateAppointFlag(BoolEnum.TRUE.getType());
                                    vo.setPrivateAppointStudentId(null);
                                }else{
                                    vo.setPrivateAppointFlag(BoolEnum.FALSE.getType());
                                    vo.setPrivateAppointStudentId(null);
                                }
                            }
                        }
                    }



                }
                if(mbDezy&&!mbDyzy){
                    throw new AppException("填报民办调剂志愿1，必须先填第一志愿");
                }
                if (mbDszy && !mbDezy) {
                    throw new AppException("填报民办调剂志愿2，必须先填调剂志愿1");
                }
            }
            //将有房产或有户籍的设置为第一梯队
            if(eduStudentEnrollPre.getHouseHoldVO()!=null||!CollectionUtils.isEmpty(eduStudentEnrollPre.getHouseList())){
                vo.setConfigNameNum(1);
            }else{
                vo.setConfigNameNum(2);
            }
            boolean checkHouse=false;
            boolean checkHousehold=false;
            boolean abcFlag=false;
            if("0".equals(vo.getSchoolNature())) {
                if ("A".equals(vo.getConfigName())) {
                    if (StringUtils.isBlank(vo.getHouseholdId())) {
                        throw new AppException("报名A类需要在该公办学校学区有户籍");
                    }
                    if (StringUtils.isBlank(vo.getHouseId())) {
                        throw new AppException("报名A类需要在该公办学校学区有房产");
                    }
                    checkHouse = true;
                    checkHousehold = true;
                    abcFlag = true;
                }
                if ("B".equals(vo.getConfigName())) {
                    if (StringUtils.isBlank(vo.getHouseholdId())) {
                        throw new AppException("报名B类需要在该公办学校学区有户籍");
                    }
                    checkHousehold = true;
                    abcFlag = true;
                }
                if ("C".equals(vo.getConfigName())) {
                    if (StringUtils.isBlank(vo.getHouseId())) {
                        throw new AppException("报名C类需要在该公办学校学区有房产");
                    }
                    checkHouse = true;
                    abcFlag = true;
                }
                if ("特殊户口".equals(vo.getConfigName())) {
                    if (StringUtils.isBlank(vo.getHouseholdId())) {
                        throw new AppException("报名特殊户口需要在该公办学校学区有户籍");
                    }
                    checkHousehold = true;
                    abcFlag = true;
                }
                if (checkHouse) {
                    //校验房产是否存在
                    if(StringUtils.isBlank(vo.getHouseId())){
                        throw new AppException("请传入报名使用的房产id");
                    }
                    List<EduHouseVO> houseVOList=eduStudentEnrollPre.getHouseList();
                    EduHouseVO eduHouse =null;
                    for(EduHouseVO eduHouseVO:houseVOList){
                        if(eduHouseVO.getHouseId().equalsIgnoreCase(vo.getHouseId())){
                            eduHouse=eduHouseVO;
                            break;
                        }else{
                            if(!CollectionUtils.isEmpty(eduHouseVO.getGyHouseIdList())){
                                if(eduHouseVO.getGyHouseIdList().contains(vo.getHouseId())){
                                    eduHouse=eduHouseVO;
                                    break;
                                }
                            }
                        }
                    }
                    if (eduHouse == null || !BoolEnum.TRUE.getType().equals(eduHouse.getStatus()) || !AuthStatusEnum.YES.getType().equals(eduHouse.getAuthStatus())) {
                        throw new AppException("不是有效的房产");
                    }
                    if (BoolEnum.TRUE.getType().equals(eduHouse.getUseFlag())) {
                        throw new AppException("该房产3年内已使用，如有疑问，请致电联系");
                    }
                    vo.setRealProperty(eduHouse.getRealProperty());
                    vo.setRealPropertyNum(eduHouse.getRealPropertyNum());
                    vo.setRegisterDate(eduHouse.getRegisterDate());
                    vo.setQlrName(eduHouse.getQlrName());
                    vo.setHouseAdreess(eduHouse.getAddress());
                    //判断是否只能房产所有者才能报名
                    CommonVO commonVO = new CommonVO();
                    commonVO.setParamKey("only_my_house_enroll");
                    String onlyflag = commonIntegration.getValueByKey(commonVO);
                    boolean canEnroll=false;
                    if("0".equalsIgnoreCase(onlyflag)||"false".equalsIgnoreCase(onlyflag)){
                        canEnroll=true;
                    }else{
                        if(BoolEnum.TRUE.getType().equalsIgnoreCase(eduStudentEnrollPre.getAgentEnrollFlag())){
                            //代理报名
                            canEnroll=true;
                        }else {
                            List<EduHouseDetailVO> detailVOList = eduHouse.getDetailList();
                            SessionInfo sessionInfo = SessionUtil.getSessionInfo();
                            if (sessionInfo == null) {
                                throw new AppException("请先登录后再操作");
                            }
                            for (EduHouseDetailVO detailVO : detailVOList) {
                                if (StringUtils.isBlank(detailVO.getIdcard())) {
                                    if (SessionUtil.getFullName().equalsIgnoreCase(detailVO.getFullName())) {
                                        canEnroll = true;
                                        break;
                                    }
                                } else {
                                    if (SessionUtil.getSessionInfo().getIdcard().equalsIgnoreCase(detailVO.getIdcard())) {
                                        canEnroll = true;
                                        break;
                                    }
                                    if (vo.getIdcard().equalsIgnoreCase(detailVO.getIdcard())) {
                                        canEnroll = true;
                                        break;
                                    }
                                }
                            }
                        }

                    }
                    if(!canEnroll){
                        throw new AppException("您不是房产所有者，不能报名，请使用房产证上的权利人账号登录系统进行报名。");
                    }
                }
                if (checkHousehold) {
                    if(StringUtils.isBlank(vo.getHouseholdId())){
                        throw new AppException("请传入报名使用的户籍id");
                    }
                    //校验户籍是否存在
                    EduHousehold eduHousehold = eduHouseholdService.getById(vo.getHouseholdId());
                    if (eduHousehold == null || !BoolEnum.TRUE.getType().equals(eduHousehold.getStatus()) || !AuthStatusEnum.YES.getType().equals(eduHousehold.getAuthStatus())) {
                        throw new AppException("不是有效的户籍");
                    }
                    vo.setQysj(eduHousehold.getQysj());
                    vo.setHouseholdAddress(eduHousehold.getAddress());
                }
                List<EduStudentEnrollBeforeVO> boforeList=eduStudentEnrollPre.getEnrollBeforeList();
                if (abcFlag) {
                    EduStudentEnrollBeforeVO before = null;
                    if (vo.getEnrollBeforeId() == null) {
                       throw new AppException("一键报名id不能为空！");
                    }
                    if(!CollectionUtils.isEmpty(boforeList)){
                        for(EduStudentEnrollBeforeVO beforeVO:boforeList){
                            if(beforeVO.getEnrollBeforeId().equals(vo.getEnrollBeforeId())){
                                before=beforeVO;
                            }
                        }
                    }
                    if (before == null) {
                        throw new AppException("系统校验到报名条件存在不符合情况，请退出到当年适龄子女，重新一键报名");
                    } else {
                        if (checkHouse) {
                            if (!vo.getHouseId().equals(before.getHouseId())) {
                                throw new AppException("系统校验到房产不符合情况，请退出到当年适龄子女，重新一键报名");
                            }
                        }
                        if (checkHousehold) {
                            if (!vo.getHouseholdId().equals(before.getHouseholdId())) {
                                throw new AppException("系统校验到户籍不符合情况，请退出到当年适龄子女，重新一键报名");
                            }
                        }
                    }
                }
                if ("E".equals(vo.getConfigName())) {
                    //判断学校是否城区E
                    if (eduSchool.getAppointFlag()) {
                        //判断是否E类名单
                        List<EduAppointStudentVO> appointStudentVOList =eduStudentEnrollPre.getCqeList();
                        EduAppointStudentVO eduAppointStudent = null;
                        if(!CollectionUtils.isEmpty(appointStudentVOList)){
                            for(EduAppointStudentVO appointStudentVO:appointStudentVOList){
                                if(appointStudentVO.getSchoolId().equalsIgnoreCase(vo.getSchoolId())
                                    &&BoolEnum.TRUE.getType().equalsIgnoreCase(appointStudentVO.getStatus())){
                                    eduAppointStudent=appointStudentVO;
                                    break;
                                }
                            }
                        }

                        if (eduAppointStudent != null) {
                            vo.setAppointFlag(BoolEnum.TRUE.getType());
                            vo.setAppointStudentId(eduAppointStudent.getAppointStudentId());
                            //判断是否4个初中内存在ABC，存在的话不能报E
                            CommonVO commonVO=new CommonVO();
                            commonVO.setParamKey("cq_school_e");
                            commonVO.setAreaCode(vo.getAreaCode());
                            String schoolStr = commonIntegration.getValueByKeyAndArea(commonVO);
                            List<String> eSchoolList = new ArrayList<>();
                            if (StringUtils.isNotBlank(schoolStr)) {
                                eSchoolList = Arrays.stream(schoolStr.split(",")).map(s -> s).collect(Collectors.toList());
                            }
                            if (eSchoolList.contains(eduAppointStudent.getSchoolId())) {
                                List<String> configNameList = new ArrayList<>();
                                configNameList.add("A");
                                configNameList.add("B");
                                configNameList.add("C");
                                configNameList.add("特殊户口");
                                if(!CollectionUtils.isEmpty(boforeList)){
                                    for(EduStudentEnrollBeforeVO beforeVO:boforeList){
                                        if(configNameList.contains(beforeVO.getConfigName())
                                            &&eSchoolList.contains(beforeVO.getSchoolId())){
                                            throw new AppException("请选择一键报名中A、B、C、特殊户口的学校进行报名");
                                        }
                                    }
                                }

                            }
                        } else {
                            QueryWrapper<EduBreakThrough> queryWrapper = new QueryWrapper<>();
                            queryWrapper.lambda().eq(EduBreakThrough::getSchoolId, vo.getSchoolId())
                                    .eq(EduBreakThrough::getYear, eduStudentEnrollPre.getYear())
                                    .eq(EduBreakThrough::getStatus, BoolEnum.TRUE.getType());
                            if (eduBreakThroughService.count(queryWrapper) <= 0) {
                                throw new AppException("不符合该学校E类资格，如有疑问，请拨打学校电话了解详细情况");
                            }

                        }
                        abcFlag = true;
                    } else {
                        vo.setAppointFlag(BoolEnum.FALSE.getType());
                        vo.setAppointStudentId(null);
                    }
                } else {
                    vo.setAppointFlag(BoolEnum.FALSE.getType());
                    vo.setAppointStudentId(null);
                }
                boolean xjFlag = false;
                boolean hjFlag = false;
                boolean fcFlag = false;
                boolean fcHasFlag = false;
                boolean wqFlag = false;
                boolean wqHasFlag = false;
                boolean sbFlag = false;
                boolean qyFlag = false;
                boolean jjzFlag = false;
                if (!abcFlag && StringUtils.isNotBlank(vo.getFormId())) {
                    //其他类型字段校验必填
                    List<EduFormEnrollParamsVO> paramsList = new ArrayList<>();
                    if (BoolEnum.TRUE.getType().equals(vo.getSpecialFlag())) {
                        //特殊通道人员，目前没有自定义表单
                    } else {
                        Map<String,List<EduFormEnrollParamsVO>>  paramsMap=eduSchool.getFormParams();
                        paramsList = paramsMap.get(vo.getFormId());
                    }
                    if (!CollectionUtils.isEmpty(configList)) {
                        BeanWrapper beanWrapper = new BeanWrapperImpl(vo);
                        if(!CollectionUtils.isEmpty(paramsList)){
                            for (EduFormEnrollParamsVO config : paramsList) {
                                if (StringUtils.equals(config.getPropertyType(), "6")) {
                                    fcHasFlag = true;
                                }
                                if (StringUtils.equals(config.getPropertyType(), "7")) {
                                    wqHasFlag = true;
                                }
                                if (BoolEnum.TRUE.getType().equals(config.getRequiredFlag())) {
                                    String param = config.getParam();
                                    if (StringUtils.equals(config.getPropertyType(), "1") ||
                                            StringUtils.equals(config.getPropertyType(), "11") ||
                                            StringUtils.equals(config.getPropertyType(), "2") ||
                                            StringUtils.equals(config.getPropertyType(), "3")) {
                                        if (StringUtils.isNotBlank(param) && beanWrapper.isWritableProperty(param)) {
                                            if (ObjectUtils.isEmpty(beanWrapper.getPropertyValue(param))) {
                                                throw new AppException(config.getProperty() + "必填");
                                            }
                                        }
                                    } else if (StringUtils.equals(config.getPropertyType(), "4")) {
                                        xjFlag = true;
                                    } else if (StringUtils.equals(config.getPropertyType(), "5")) {
                                        hjFlag = true;
                                    } else if (StringUtils.equals(config.getPropertyType(), "6")) {
                                        fcFlag = true;
                                    } else if (StringUtils.equals(config.getPropertyType(), "7")) {
                                        wqFlag = true;

                                    } else if (StringUtils.equals(config.getPropertyType(), "8")) {
                                        sbFlag = true;
                                    } else if (StringUtils.equals(config.getPropertyType(), "9")) {
                                        qyFlag = true;
                                    } else if (StringUtils.equals(config.getPropertyType(), "10")) {

                                        jjzFlag = true;

                                    }
                                }
                            }
                        }

                    }
                }
                if(!abcFlag&&!wqHasFlag&&!fcHasFlag&&!"直升".equalsIgnoreCase(vo.getConfigTypeName())&&!BoolEnum.TRUE.getType().equalsIgnoreCase(vo.getSpecialFlag())){
                    //除了ABC特殊户口和网签、直升、特殊通道之外，需要校验父母的居住证信息
                    boolean checkFlag=false;
                    if(eduStudentEnrollPre.getHouseHoldVO()==null){
                        checkFlag=true;

                    }else{
                        if (!"330783".equals(eduStudentEnrollPre.getHouseHoldVO().getAreaCode())){
                            //不是东阳户籍，要判断居住证
                            checkFlag=true;
                        }
                    }
                    if (checkFlag){
                        if(eduStudentEnrollPre.getFmJzzFlag()==null){
                            eduStudentEnrollPre.setFmJzzFlag(false);
                        }
                        if(eduStudentEnrollPre.getHouseholdFmFlag()==null){
                            eduStudentEnrollPre.setHouseholdFmFlag(false);
                        }
                        if(eduStudentEnrollPre.getGatFlag()==null){
                            eduStudentEnrollPre.setGatFlag(false);
                        }
                        //孩子的居住证，父母有东阳户籍或者孩子有东阳学籍，可以使用
                        if(!eduStudentEnrollPre.getGatFlag()&&!eduStudentEnrollPre.getCanEnrollFlag()&&!eduStudentEnrollPre.getHouseholdFmFlag()&&eduStudentEnrollPre.getStudentStatusVO()==null){
                            throw new AppException("该类别(" + vo.getConfigName() + ")报名需要孩子的父母一方有居住证");
                        }
                    }
                }
                //判断是否有网签
                if (wqFlag && StringUtils.isBlank(vo.getNetsignId())) {
                    throw new AppException("该类别(" + vo.getConfigName() + ")需要有购房合同才能报名");
                }
                List<EduNetsignVO> netsignVOList = eduStudentEnrollPre.getWqList();
                if (!CollectionUtils.isEmpty(netsignVOList)) {
                    vo.setNetsignFlag(BoolEnum.FALSE.getType());
                    for(EduNetsignVO netsignVO:netsignVOList){
                        if (netsignVO.getNetsignId().equalsIgnoreCase(vo.getNetsignId())) {
                            vo.setNetsignFlag(BoolEnum.TRUE.getType());
                            break;
                        }
                    }
                    if (wqFlag&&!BoolEnum.TRUE.getType().equalsIgnoreCase(vo.getNetsignFlag())) {
                        throw new AppException("该类别(" + vo.getConfigName() + ")需要有购房合同才能报名");
                    }
                } else {
                    if (wqFlag) {
                        throw new AppException("该类别(" + vo.getConfigName() + ")需要有购房合同才能报名");
                    }
                    vo.setNetsignFlag(BoolEnum.FALSE.getType());
                }
                //获取学籍信息
                if (eduStudentEnrollPre.getStudentStatusVO() != null) {
                    vo.setStudentStatusId(eduStudentEnrollPre.getStudentStatusVO().getStudentStatusId());
                } else {
                    if (xjFlag) {
                        throw new AppException("该类别需要东阳学籍信息才能报名" + vo.getConfigName());
                    }
                }
                //判断是否有参保
                if (sbFlag && StringUtils.isBlank(vo.getSocialSecurityId())) {
                    throw new AppException("该类别(" + vo.getConfigName() + ")需要在东阳正常参保职工养老保险并且连续缴费满6个月以上才能报名");
                }
                List<EduSocialSecurityVO> sbList=eduStudentEnrollPre.getCbList();
                if (!CollectionUtils.isEmpty(sbList)) {
                    vo.setCbFlag(BoolEnum.FALSE.getType());
                    for (EduSocialSecurityVO socialSecurityVO : sbList) {
                        if (socialSecurityVO.getSocialSecurityId().equalsIgnoreCase(vo.getSocialSecurityId())) {
                            vo.setCbFlag(BoolEnum.TRUE.getType());
                            vo.setCbTotal(socialSecurityVO.getTotal());
                            break;
                        }
                    }
                    if (sbFlag && !BoolEnum.TRUE.getType().equalsIgnoreCase(vo.getCbFlag())) {
                        throw new AppException("该类别(" + vo.getConfigName() + ")需要在东阳正常参保职工养老保险并且连续缴费满6个月以上才能报名");
                    }

                } else {
                    if (sbFlag) {
                        throw new AppException("该类别(" + vo.getConfigName() + ")需要在东阳正常参保职工养老保险并且连续缴费满6个月以上才能报名");
                    }
                    vo.setCbFlag(BoolEnum.FALSE.getType());
                }
                //判断是否有居住证
                if (jjzFlag && StringUtils.isBlank(vo.getLivePlaceId())) {
                    throw new AppException("该类别(" + vo.getConfigName() + ")需要东阳居住证信息才能报名");
                }
                List<EduLivePlaceVO> jzzList=eduStudentEnrollPre.getJzzList();
                if (!CollectionUtils.isEmpty(jzzList)) {
                    vo.setLivePlaceFlag(BoolEnum.FALSE.getType());
                    for (EduLivePlaceVO livePlaceVO : jzzList) {
                        if (livePlaceVO.getLivePlaceId().equalsIgnoreCase(vo.getLivePlaceId())) {
                            vo.setLivePlaceFlag(BoolEnum.TRUE.getType());
                            break;
                        }
                    }
                    if (jjzFlag && !BoolEnum.TRUE.getType().equalsIgnoreCase(vo.getLivePlaceFlag())) {
                        throw new AppException("该类别(" + vo.getConfigName() + ")需要东阳居住证信息才能报名");
                    }
                } else {
                    vo.setLivePlaceFlag(BoolEnum.FALSE.getType());
                    if (jjzFlag) {
                        throw new AppException("该类别(" + vo.getConfigName() + ")需要东阳居住证信息才能报名");
                    }
                }
                //开办企业
                if (qyFlag && StringUtils.isBlank(vo.getCompanyId())) {
                    throw new AppException("该类别(" + vo.getConfigName() + ")需要在东阳有开办企业才能报名");
                }
                List<EduCompanyVO> qyList=eduStudentEnrollPre.getQyList();
                if (!CollectionUtils.isEmpty(qyList)) {
                    vo.setQyFlag(BoolEnum.FALSE.getType());
                    for(EduCompanyVO companyVO:qyList){
                        if (companyVO.getCompanyId().equalsIgnoreCase(vo.getCompanyId())) {
                            vo.setQyFlag(BoolEnum.TRUE.getType());
                            break;
                        }
                    }
                    if (qyFlag&&!BoolEnum.TRUE.getType().equalsIgnoreCase(vo.getQyFlag())) {
                        throw new AppException("该类别(" + vo.getConfigName() + ")需要在东阳有开办企业才能报名");
                    }
                } else {
                    vo.setQyFlag(BoolEnum.FALSE.getType());
                    if (qyFlag) {
                        throw new AppException("该类别(" + vo.getConfigName() + ")需要在东阳有开办企业才能报名");
                    }
                }
                //户籍
                if (hjFlag) {
                    if (eduStudentEnrollPre.getHouseHoldVO()==null) {
                        throw new AppException("该类别需要有东阳户籍信息才能报名" + vo.getConfigName());
                    }
                }
                //房产
                if (fcFlag && StringUtils.isBlank(vo.getHouseId())) {
                    throw new AppException("该类别(" + vo.getConfigName() + ")需要有东阳房产信息才能报名");
                }
                List<EduHouseVO> houseList = eduStudentEnrollPre.getHouseList();
                if (!CollectionUtils.isEmpty(houseList)) {
                    if (fcFlag) {
                        boolean hFlag = false;
                        for (EduHouseVO houseVO : houseList) {
                            if (houseVO.getHouseId().equalsIgnoreCase(vo.getHouseId())) {
                                hFlag = true;
                                break;
                            }
                        }
                        if (!hFlag) {
                            throw new AppException("该类别(" + vo.getConfigName() + ")需要有东阳房产信息才能报名");
                        }
                    }
                } else {
                    if (fcFlag) {
                        throw new AppException("该类别(" + vo.getConfigName() + ")需要有东阳房产信息才能报名");
                    }
                }

            }else if("1".equals(vo.getSchoolNature())){
                //民办 只有东阳户籍才能报民办
                /***
                if(!"直升".equalsIgnoreCase(vo.getConfigTypeName())&&!eduStudentEnrollPre.getGatFlag()){
                    if(eduStudentEnrollPre.getHouseHoldVO()==null){
                        throw new AppException("您的孩子户籍不在东阳市内，不能报名民办学校");
                    }
                }***/
                if(eduStudentEnrollPre.getHouseHoldVO()!=null||!CollectionUtils.isEmpty(eduStudentEnrollPre.getHouseList())||eduStudentEnrollPre.getGatFlag()){
                    vo.setConfigNameNum(1);
                }else{
                    vo.setConfigNameNum(2);
                }
            }
        }
    }
    private void checkFull(SaveStudentEnrollVO vo,EduSchoolConfigVO schoolConfig){
        if(schoolConfig==null){
            throw new AppException(vo.getYear()+"年度报名学校未配置招生计划，如有疑问，请拨打学校电话了解详细情况");
        }
        //计划人数
        Integer enrolment = 0;
        enrolment = schoolConfig.getEnrolment() == null ? 0 : schoolConfig.getEnrolment();
        //已录取+报名人数
        QueryWrapper<EduStudentEnroll> lqQuery = new QueryWrapper<>();
        List<String> enrollStatusList=new ArrayList<>();
        enrollStatusList.add(EnrollStatusFormalEnum.dsh.getType());
        enrollStatusList.add(EnrollStatusFormalEnum.dlq.getType());
        enrollStatusList.add(EnrollStatusFormalEnum.lq.getType());
        lqQuery.lambda().in(EduStudentEnroll::getEnrollStatus, enrollStatusList)
                .eq(EduStudentEnroll::getYear, vo.getYear())
                .eq(EduStudentEnroll::getSchoolId, vo.getSchoolId());
        if ("1".equals(vo.getSchoolNature()) || "2".equals(vo.getSchoolNature())) {
            //民办
            lqQuery.lambda().eq(EduStudentEnroll::getConfigTypeName, vo.getConfigTypeName());
        }
        Long lqCount = eduStudentEnrollService.count(lqQuery);
        if (lqCount >= enrolment) {
            throw new AppException("报名已满");
        }
    }
}
